Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithm Analysis Graph Algorithms

Similar presentations


Presentation on theme: "Data Structures and Algorithm Analysis Graph Algorithms"— Presentation transcript:

1 Data Structures and Algorithm Analysis Graph Algorithms
Lecturer: Ligang Dong Tel: , Office: SIEE Building 305

2 Definitions A graph G=(V, E) consists of a set of vertices, V, and a set of edges, E. Each edge is a pair (v, w), where v, wV. Edges are sometimes referred to as arcs. If the pair is ordered, then the graph is directed. Vertex w is adjacent to v if and only if (v, w)E. In an undirected graph with edge {v, w}, w is adjacent to v and v is adjacent to w. Some times an edge has a third component, known as either a weight or a cost.

3 Definitions A path in a graph is a sequence of vertices w1, w2, w3, …, wN such that (wi, wi+1)E for 1i<N. The length of such a path is the number of edges on the path, which is equal to N-1. We allow a path from a vertex to itself; if this path contains no edges, then the path length is 0. If the graph contains an edge (v, v) from a vertex to itself, then the path v, v is sometime referred to as a loop. The graphs we will consider will generally be loopless. A simple path is a path such that all vertices are distinct, except that the first and the last could be the same.

4 Definitions Path: (V1, V4, V3, V5) (V1, V2, V5) (V1, V2, V3, V5)
(V1, V3, V4 , V1, V2) is not a simple path

5 Definitions A cycle in a directed graph is a path of length at least 1 such that w1=wn; this cycle is simple if the path is simple. For undirected graphs, we require that the edges be distinct; that is, the path u, v, u in an undirected graph should not be considered a cycle, because (u, v) and (v, u) are the same edge. In a directed graph, these are different edges, so it makes sense to call this a cycle. A directed graph is acyclic if it has no cycles.

6 Definitions Cycle and Simple cycle : (V1, V3, V4 , V1)

7 Definitions An undirected graph is connected if there is a path from every vertex to every other vertex. A directed graph with this property is called strongly connected. If a directed graph is not strongly connected, but the underlying graph (without direction to the arcs) is connected, then the graph is said to be weakly connected. A complete graph is a graph in which there is an edge between every pair of vertices. weakly connected

8 Definitions In an undirected graph, the degree of a vertex is the number of edges connected to this vertex. In an directed graph, the outdegree of a vertex is the number of edges that start from this vertex, and the indegree is the number of edges that end at this vertex.

9 Representation of Graphs
Suppose, for now, that we can number the vertices, starting at 1; that is, V={1, 2, …, n} One simple way to represent a graph is to use a two-dimensional array this is known as a adjacency matrix representation. For each edge (u, v), we set A[u][v]=1; otherwise the entry in the array is 0. If the edge has a weight associated with it, then we can set A[u][v] equal to the weight and use either a very large or a very small weight as a sentinel to indicate nonexistent edges.

10 Representation of Graphs
adjacency matrix

11 Representation of Graphs
If the number of edges in a graph is very small, a better solution is an adjacency list representation. For each vertex, we keep a list of all adjacent vertices: The leftmost structure is merely an array of header cells. If the edges have weights, then this additional information is also stored in the cells.

12 Representation of Graphs
adjacency list

13 Topological Sort A topological sort is an ordering of vertices in a directed acyclic graph, such that if there is a path from vi to vj, then vj appears after vi in the ordering. See Figure 9.3: A directed edge (v, w) indicates that course v must be completed before course w may be attempted. A topological ordering of these courses is any course sequence that does not violate the prerequisite requirement.

14 Topological Sort It is clear that a topological ordering is not possible if the graph has a cycle, since for two vertices v and w on the cycle, v precedes w and w precedes v. The ordering is not necessarily unique; any legal ordering will do. A simple algorithm to find a topological ordering is first to find any vertex with no incoming edges. We can then print this vertex, and remove it, along with this edge, from the graph. Then we apply this same strategy to the rest of the graph. Thus, we compute the indegrees of all vertices in the graph, and look for a vertex with indegree 0 that has not already been assigned a topological number.

15 Topological Sort Example: Course prerequisite requirement C1 N/A C2 C3

16 Topological Sort Example: Possible order is C1,C2,C3,C4,C5,C8,C9,C7,C6,or C1,C2,C3,C8,C4,C5,C9,C7,C6。

17 Topological Sort C5 C2 C6 C4 C3 C7 C1 C8 C9 Topological order :C1

18 Topological Sort C5 C2 C6 C4 C3 C7 C8 C9 Topological order :C1C2

19 Topological Sort C5 C6 C4 C3 C7 C8 C9 Topological order :C1C2C3

20 Topological Sort C5 C6 C4 C7 C8 C9 Topological order :C1C2C3C5

21 Topological Sort C6 C4 C7 C8 C9 Topological order :C1C2C3C5C8

22 Topological Sort C6 C4 C7 C9 Topological order :C1C2C3C5C8C9

23 Topological Sort C6 C4 C7 Topological order :C1C2C3C5C8C9C4

24 Topological Sort C6 C7 Topological order :C1C2C3C5C8C9C4C6C7

25 The All-Pairs Shortest Path Problem
Let G=(V, E) be a directed graph in which each edge (i, j) has a non-negative length l[i, j]. If there is no edge from vertex i to vertex j, then l[i, j]=. The problem is to find the distance from each vertex to all other vertices, where the distance from vertex x to vertex y is the length of a shortest path from x to y. For simplicity, we will assume that V={1, 2, …, n}. Let i and j be two different vertices in V. Define to be the length of a shortest path from i to j that does not pass through any vertex in {k+1, k+2, …, n}.

26 The All-Pairs Shortest Path Problem
is the length of a shortest path from i to j that does not pass through any vertex except possibly vertex 1 is the length of a shortest path from i to j that does not pass through any vertex except possibly vertex 1 or vertex 2 or both is the length of a shortest path from i to j, i.e. the distance from i to j

27 The All-Pairs Shortest Path Problem
We can compute recursively as follows:

28 The All-Pairs Shortest Path Problem
Floyd Algorithm: use n+1 matrices D0, D1, D2, …, Dn of dimension nn to compute the lengths of the shortest constrained paths. Initially, we set D0[i, i]=0, D0[i, j]=l[i, j] if ij and (i, j) is an edge in G; otherwise D0[i, j]=. We then make n iterations such that after the kth iteration, Dk[i, j] contains the value of a shortest length path from vertex i to vertex j that does not pass through any vertex numbered higher than k.

29 The All-Pairs Shortest Path Problem
Thus, in the kth iteration, we compute Dk[i, j] using the formula Dk[i, j]=min{Dk-1[i, j], Dk-1[i, k]+Dk-1[k, j]}

30 The All-Pairs Shortest Path Problem
Example:

31 The All-Pairs Shortest Path Problem
Example:

32 The All-Pairs Shortest Path Problem
Example:

33 The All-Pairs Shortest Path Problem
Example:

34 The All-Pairs Shortest Path Problem
Example:

35 The All-Pairs Shortest Path Problem
Example:

36 The All-Pairs Shortest Path Problem
Example:

37 The All-Pairs Shortest Path Problem
Input: An nn matrix l[1…n, 1…n] such that l[i, j] is the length of the edge (i, j) in a directed graph G=({1, 2, …, n}, E); Output: A matrix D with D[i, j]=the distance from i to j; 1. Dl; 2. for k1 to n for i1 to n for j1 to n D[i, j]=min{D[i, j], D[i, k]+D[k, j]); end for; end for; 8. end for;

38 The All-Pairs Shortest Path Problem
What is the time and space complexity of the FLOYD algorithm?

39 The All-Pairs Shortest Path Problem
The running time of FLOYD algorithm is (n3) and its space complexity is (n2).

40 The Shortest Path Problem
Let G=(V, E) be a directed graph in which each edge has a nonnegative length, and a distinguished vertex s called the source. The single-source shortest path problem, or simply the shortest path problem, is to determine the distance from s to every other vertex in V, where the distance from vertex s to vertex x is defined as the length of a shortest path from s to x. For simplicity, we will assume that V={1, 2, …, n} and s=1. This problem can be solved using a greedy technique known as Dijkstra’s algorithm.

41 The Shortest Path Problem
The set of vertices is partitioned into two sets X and Y so that X is the set of vertices whose distance from the source has already been determined, while Y contains the rest vertices. Thus, initially X={1} and Y={2, 3, …, n}. Associated with each vertex y in Y is a label [y], which is the length of a shortest path that passes only through vertices in X. Thus, initially

42 The Shortest Path Problem
At each step, we select a vertex yY with minimum  and move it to X, and  of each vertex wY that is adjacent to y is updated indicating that a shorter path to w via y has been discovered. The above process is repeated until Y is empty. Finally,  of each vertex in X is the distance from the source vertex to this one.

43 The Shortest Path Problem
Example:

44 The Shortest Path Problem
i 1 2 3 4 5 6 path[i] v1 dist[i] 50 10 45 dist[i] is [i]

45 The Shortest Path Problem
i 1 2 3 4 5 6 path[i] v1  v1v3 dist[i] 50 10 25 45

46 The Shortest Path Problem
i 1 2 3 4 5 6 path[i] v1  v1v3  v1v3v4 dist[i] 45 10 25

47 The Shortest Path Problem
i 1 2 3 4 5 6 path[i] v1  v1v3v4v2  v1v3  v1v3v4  v1v5 dist[i] 45 10 25

48 The Shortest Path Problem
Input: A weighted directed graph G=(V, E), where V={1, 2, …, n}; Output: The distance from vertex 1 to every other vertex in G; 1. X={1}; YV-{1}; [1]0; 2. for y2 to n if y is adjacent to 1 then [y]length[1, y]; else [y]; end if; 6. end for; 7. for j1 to n-1 Let yY be such that [y] is minimum; XX{y}; //add vertex y to X YY-{y}; //delete vertex y from Y for each edge (y, w) if wY and [y]+length[y, w]<[w] then [w][y]+length[y, w]; end for; 15. end for;

49 The Shortest Path Problem
What’s the performance of the DIJKSTRA algorithm? Time Complexity?

50 The Shortest Path Problem
Given a directed graph G with nonnegative weights on its edges and a source vertex s, Algorithm DIJKSTRA finds the length of the distance from s to every other vertex in (n2) time.

51 Minimum Cost Spanning Trees (Kruskal’s Algorithm)
Let G=(V, E) be a connected undirected graph with weights on its edges. A spanning tree (V, T) of G is a subgraph of G that is a tree. If G is weighted and the sum of the weights of the edges in T is minimum, then (V, T) is called a minimum cost spanning tree or simply a minimum spanning tree.

52 Minimum Cost Spanning Trees (Kruskal’s Algorithm)
Kruskal’s algorithm works by maintaining a forest consisting of several spanning trees that are gradually merged until finally the forest consists of exactly one tree. The algorithm starts by sorting the edges in nondecreasing order by weight.

53 Minimum Cost Spanning Trees (Kruskal’s Algorithm)
Next, starting from the forest (V, T) consisting of the vertices of the graph and none of its edges, the following step is repeated until (V, T) is transformed into a tree: Let (V, T) be the forest constructed so far, and let eE-T be the current edge being considered. If adding e to T does not create a cycle, then include e in T; otherwise discard e. This process will terminate after adding exactly n-1 edges.

54 Minimum Cost Spanning Trees (Kruskal’s Algorithm)
Example: 16 5 11 6 18

55 Minimum Cost Spanning Trees (PRIM’s Algorithm)
Example: U U U U U 1 5 3 2 4

56 Graph Traversal In some cases, what is important is that the vertices are visited in a systematic order, regardless of the input graph. Usually, there are two methods of graph traversal: Depth-first search Breadth-first search

57 Depth-First Search Let G=(V, E) be a directed or undirected graph.
First, all vertices are marked unvisited. Next, a starting vertex is selected, say vV, and marked visited. Let w be any vertex that is adjacent to v. We mark w as visited and advance to another vertex, say x, that is adjacent to w and is marked unvisited. Again, we mark x as visited and advance to another vertex that is adjacent to x and is marked unvisited.

58 Depth-First Search This process of selecting an unvisited vertex adjacent to the current vertex continues as deep as possible until we find a vertex y whose adjacent vertices have all been marked visited. At this point, we back up to the most recently visited vertex, say z, and visit an unvisited vertex that is adjacent to z, if any. Continuing this way, we finally return back to the starting vertex v. The algorithm for such a traversal can be written using recursion.

59 Depth-First Search Example: Result is:A、B、C、F、D、G、E、H、I。 4

60 Depth-First Search When the search is complete, if all vertices are reachable from the start vertex, a spanning tree called the depth-first search spanning tree is constructed whose edges are those inspected in the forward direction, i.e., when exploring unvisited vertices. As a result of the traversal, the edges of an undirected graph G are classified into the following two types: Tree edges: edges in the depth-first search tree. Back edges: all other edges.

61 Depth-First Search Input: An undirected graph G=(V, E);
Output: Preordering of the vertices in the corresponding depth-first search tree. 1. predfn0; 2. for each vertex vV Mark v unvisited; 4. end for; 5. for each vertex vV if v is marked unvisited then dfs(v); 7. end for; dfs(v) 1. Mark v visited; 2. predfnpredfn+1; 3. for each edge (v, w)E if w is marked unvisited then dfs(w); 5. end for;

62 Breadth-First Search When we visit a vertex v, we next visit all vertices adjacent to v. This method of traversal can be implemented by a queue to store unexamined vertices.

63 Breadth-First Search Example: Result is:A、B、D、E、C、G、F、H、I。

64 Homework #10 10.1 Find a topological ordering for the graph
10.2 Find the shortest path from A to all other vertices for the graph

65 Homework #10 10.3 Find a minimum spanning tree for the graph using both Prim's and Kruskal's algorithms


Download ppt "Data Structures and Algorithm Analysis Graph Algorithms"

Similar presentations


Ads by Google