data

advertisement

1.What’s a graph ?

Graphs (also known as Networks) are very powerful structures and find their applications in path-finding, visibility determination, soft-bodies using mass-spring systems and probably a lot more. A graph is similar to a tree, but it imposes no restrictions on how nodes are connected to each other. In fact each node can point to another node or even multiple nodes at once.

A node is represented by the GraphNode class, and the connections between GraphNode objects are modeled by the GraphArc class. The arc has only one direction (uni-directional) and points from one node to another so you are only allowed to go from A to B and not in the opposite direction. Bi-directional connections can be simulated by creating an arc from node A to B and viceversa from B to A. Also, each arc has a weight value associated with it, which describes how costly it is to move along the arc.

This is optional though, so the default value is 1.

2.

3.

Putting it together, the graph is implemented as an uni-directional weighted graph. The Graph manages everything: it stores the nodes and the arcs in separate lists, makes sure you don’t add a node twice, or mess up the arcs (for example if you remove a node from the graph, it also scans the arc list and removes all arcs pointing to that node) and provides you with tools to traverse the graph.

4. In the mathematical field of graph theory, a spanning tree T of a connected, undirected graph G is a tree composed of all the vertices and some (or perhaps all) of the edges of G. Informally, a spanning tree of G is a selection of edges of G that form a tree

spanning every vertex. That is, every vertex lies in the tree, but no cycles (or loops) are formed. On the other hand, every bridge of G must belong to T.

A spanning tree of a connected graph G can also be defined as a maximal set of edges of G that contains no cycle, or as a minimal set of edges that connect all vertices.

In certain fields of graph theory it is often useful to find a minimum spanning tree of a weighted graph. Other optimization problems on spanning trees have also been studied, including the maximum spanning tree, the minimum tree that spans at least k vertices, the minimum spanning tree with at most k edges per vertex (Degree-Constrained Spanning Tree), the spanning tree with the largest number of leaves (closely related to the smallest connected dominating set), the spanning tree with the fewest leaves (closely related to the Hamiltonian path problem), the minimum diameter spanning tree, and the minimum dilation spanning tree.

5. DFS is an uninformed search that progresses by expanding the first child node of the search tree that appears and thus going deeper and deeper until a goal node is found, or until it hits a node that has no children. Then the search backtracks, returning to the most recent node it hasn't finished exploring. In a non-recursive implementation, all freshly expanded nodes are added to a stack for exploration.

The time and space analysis of DFS differs according to its application area. In theoretical computer science, DFS is typically used to traverse an entire graph, and takes time O(|V| + |E|), linear in the size of the graph. In these applications it also uses space O(|V| + |E|) in the worst case to store the stack of vertices on the current search path as well as the set of alreadyvisited vertices. Thus, in this setting, the time and space bounds are the same as for breadth first search and the choice of which of these two algorithms to use depends less on their complexity and more on the different properties of the vertex orderings the two algorithms produce.

For applications of DFS to search problems in artificial intelligence, however, the graph to be searched is often either too large to visit in its entirety or even infinite, and DFS may suffer from non-termination when the length of a path in the search tree is infinite. Therefore, the search is only performed to a limited depth, and due to limited memory availability one typically does not use data structures that keep track of the set of all previously visited vertices. In this case, the time is still linear in the number of expanded vertices and edges (although this number is not the same as the size of the entire graph because some vertices may be searched more than once and others not at all) but the space complexity of this variant of DFS is only proportional to the depth limit, much smaller than the space needed for searching to the same depth using breadth-first

6.

7. search. For such applications, DFS also lends itself much better to heuristic methods of choosing a likely-looking branch. When an appropriate depth limit is not known a priori, iterative deepening depth-first search applies DFS repeatedly with a sequence of increasing limits; in the artificial intelligence mode of analysis, with a branching factor greater than one, iterative deepening increases the running time by only a constant factor over the case in which the correct depth limit is known due to the geometric growth of the number of nodes per level.

8. Prim's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. The algorithm was developed in 1930 by Czech mathematician Vojtěch Jarník and later independently by computer scientist Robert C. Prim in 1957 and rediscovered by Edsger Dijkstra in 1959. Therefore it is sometimes called the DJP

algorithm, the Jarník algorithm, or the Prim-Jarník algorithm.

The algorithm continuously increases the size of a tree starting with a single vertex until it spans all the vertices.

Input: A connected weighted graph with vertices V and edges E.

Initialize: V new

= {x}, where x is an arbitrary node (starting point) from V, E new

= {}

Repeat until V new

= V: o

Choose edge (u,v) with minimal weight such that u is in V new

and v is not (if there are multiple edges with the same weight, choose arbitrarily but consistently) o

Add v to V new

, add (u, v) to E new

Output: V new

and E new

describe a minimal spanning tree

9.

Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). Kruskal's algorithm is an example of a greedy algorithm.It works as follows:

 create a forest F (a set of trees), where each vertex in the graph is a separate tree create a set S containing all the edges in the graph while S is nonempty o remove an edge with minimum weight from S o if that edge connects two different trees, then add it to the forest, combining two trees into a single tree o otherwise discard that edge.

At the termination of the algorithm, the forest has only one component and forms a minimum spanning tree of the graph.

This algorithm first appeared in Proceedings of the American Mathematical Society , pp. 48–50 in 1956, and was written by

Joseph Kruskal.Other algorithms for this problem include Prim's algorithm, Reverse-Delete algorithm, and Borůvka's algorithm.

10. Dijkstra's algorithm, conceived by Dutch computer scientist Edsger Dijkstra in 1959, [1] is a graph search algorithm that solves the single-source shortest path problem for a graph with nonnegative edge path costs, producing a shortest path tree.

This algorithm is often used in routing. An equivalent algorithm was developed by Edward F. Moore in 1957. [2]

Dijkstra's algorithm

Dijkstra's algorithm runtime

Search algorithm Class

Data structure

Worst case performance

Graph

O ( | E | + | V | log | V | )

This box: view • talk

For a given source vertex (node) in the graph, the algorithm finds the path with lowest cost (i.e. the shortest path) between that vertex and every other vertex. It can also be used for finding costs of shortest paths from a single vertex to a single destination vertex by stopping the algorithm once the shortest path to the destination vertex has been determined. For example, if the vertices of the graph represent cities and edge path costs represent driving distances between pairs of cities connected by a direct road,

Dijkstra's algorithm can be used to find the shortest route between one city and all other cities. As a result, the shortest path first is widely used in network routing protocols, most notably IS-IS and OSPF (Open Shortest Path First).

11. The Floyd-Warshall algorithm compares all possible paths through the graph between each pair of vertices. It is able to do this with only V 3 comparisons. This is remarkable considering that there may be up to V 2 edges in the graph, and every combination of edges is tested. It does so by incrementally improving an estimate on the shortest path between two vertices, until the estimate is known to be optimal.

Consider a graph G with vertices V , each numbered 1 through N. Further consider a function shortestPath( i , j , k ) that returns the shortest possible path from i to j using only vertices 1 through k as intermediate points along the way. Now, given this function, our goal is to find the shortest path from each i to each j using only nodes 1 through k + 1.

There are two candidates for this path: either the true shortest path only uses nodes in the set (1...

k ); or there exists some path that goes from i to k + 1, then from k + 1 to j that is better. We know that the best path from i to j that only uses nodes 1 through k is defined by shortestPath( i , j , k ), and it is clear that if there were a better path from i to k + 1 to j , then the length of this path would be the concatenation of the shortest path from i to k + 1 (using vertices in (1...

k )) and the shortest path from k + 1 to j (also using vertices in (1...

k )).

Therefore, we can define shortestPath ( i , j , k ) in terms of the following recursive formula:

This formula is the heart of Floyd Warshall. The algorithm works by first computing shortestPath ( i , j ,1) for all (i,j) pairs, then using that to find shortestPath ( i , j ,2) for all ( i , j ) pairs, etc. This process continues until k=n, and we have found the shortest path for all ( i , j ) pairs using any intermediate vertices.

Download