Download presentation
Presentation is loading. Please wait.
Published byAmi Sims Modified over 8 years ago
1
Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department
2
“Data Structures and Algorithms in Java”, 3rd Edition, Adam Drozdek, Cengage Learning, ISBN 978-9814239233 Chapter 8 Section 8.3: Shortest Paths (8.3.1 is not included) Section 8.4: Cycle Detection (8.4.1 is not included) Section 8.5: Spanning Trees Section 8.6: Connectivity Reading Assignment
3
Outline Graph Connectedness and Cycles Dijkstra’s Shortest Path Algorithm Minimum Spanning Tree Algorithms Prim’s Algorithm Kruskal’s Algorithm 3
4
Graph Connectedness and Cycles Connectedness of an Undirected Graph Connectedness of a directed Graph Algorithm for finding strongly connected components. Cycle detection algorithm in Directed Graphs. Review Questions. 4
5
Connectedness of an Undirected Graph 5 An undirected graph G = (V, E) is connected if there is a path between every pair of vertices. Although the figure below appears to be two graphs, it is actually a single graph. Clearly, G is not connected. e.g. no path between A and D. G consists of two unconnected parts, each of which is a connected sub-graph --- connected components. V = {A, B, C, D, E, F} E = {{A, B}, {A, C}, {B, C}, {D, E}, {E, F}}
6
Algorithm for Undirected Graphs Connectedness 6 A simple way to test for connectedness in an undirected graph is to use either one method call for depth-first or breadth-first traversal (i.e., not the General Traversal Algorithm) If all the vertices are visited after that one call, the graph is connected.
7
Connectedness of a Directed Graph 7 Remember that a directed graph G = (V, E) is strongly connected if there is a directed path between every pair of vertices. A simple way to test for strong connectedness is to use |V| traversals - The graph is strongly connected if all the vertices are visited in each traversal. What is the time complexity of this algorithm in O()? Weak connectedness is implemented in the Lab. V = {A, B, C, D, E, F} E = {(A, B), (B, C), (C, A), (B, E), (D, E), (E, F), (F, D)
8
Strongly Connected Components 8 A strongly connected component in a directed graph DG= is a set of vertices C (which is a subset of V) such that for every pair of vertices x,y in C, there exists a directed path from x to y and a directed path from y to x. Thus, starting at any vertex in a strongly connected component C, it is possible to reach every other vertex in C. The number of strongly connected components in a strongly connected directed graph is …..
9
Example 1 2 3 4 5 6 7 8 9
10
SCC Algorithm 1.Perform a post-order depth first search on the directed graph DG. 2.Number the depth first search tree (or forest) using the visit method of the postorder traversal. 3.Form a new graph DGr= by reversing every edge in E to form Er. 4.Perform a depth first search on the directed graph DGr, with vertices ordered in decreasing order of their numbers generated in 2. Assign a distinct component number to all visited vertices every time the DFS procedure is called from within the general traversal algorithm. What is the time complexity of the SCC Algorithm in O()? 10
11
Example 1 2 3 4 5 6 7 8 11
12
Example 12 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8
13
Pop Quiz: Run SCC Algorithm on 13 1 2 3 4 5 6
14
Cycles in a Directed Graph An easy way to detect the presence of cycles in a directed graph is to attempt a topological order traversal. This algorithm visits all the vertices of a directed graph if the graph has no cycles. In the following graph, after A is visited and removed, all the remaining vertices have in-degree of one. Thus, a topological order traversal cannot complete. This is because of the presence of the cycle { B, C, D, B}. What is the time complexity of this algorithm in O()? 14
15
Review Questions 1. Every tree is a directed, acyclic graph (DAG), but there exist DAGs that are not trees. a) How can we tell whether a given DAG is a tree? b) Devise an algorithm to test whether a given DAG is a tree. 2. Consider an acyclic, connected, undirected graph G that has n vertices. How many edges does G have? 3. In general, an undirected graph contains one or more connected components. a) Devise an algorithm that counts the number of connected components in a graph. b) Devise an algorithm that labels the vertices of a graph in such a way that all the vertices in a given connected component get the same label and vertices in different connected components get different labels. 4. Devise an algorithm that takes as input a graph, and a pair of vertices, v and w, and determines whether w is reachable from v. 15
16
Shortest Path Algorithm 16 What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem. Implementation of Dijkstra's Algorithm
17
What is the shortest path problem? 17 In an edge-weighted graph, the weight of an edge measures the cost of traveling that edge. For example, in a graph representing a network of airports, the weights could represent: distance, cost or time. Such a graph could be used to answer any of the following: What is the fastest way to get from A to B? Which route from A to B is the least expensive? What is the shortest possible distance from A to B? Each of these questions is an instance of the same problem: The shortest path problem!
18
Is the shortest path problem well defined? 18 If all the edges in a graph have non-negative weights, then it is possible to find the shortest path from any two vertices. For example, in the figure below, the shortest path from B to F is { B, A, C, E, F } with a total cost of nine. Thus, the problem is well defined for a graph that contains non- negative weights.
19
Is the shortest path problem well defined? - Cont'd 19 Things get difficult for a graph with negative weights. For example, the path D, A, C, E, F costs 4 even though the edge (D, A) costs 5 -- the longer the less costly. The problem gets even worse if the graph has a negative cost cycle. e.g. {D, A, C, D} A solution can be found even for negative-weight graphs but not for graphs involving negative cost cycles. {D, A, C, D, A, C, E, F} = 2 {D, A, C, D, A, C, D, A, C, E, F} = 0
20
The Dijkstra's Algorithm 20 Dijkstra's algorithm solves the single-source shortest path problem for a non-negative weights graph. It finds the shortest path from an initial vertex, say s, to all the other vertices.
21
The Dijkstra's Algorithm Cont'd 21 // Let V be the set of all vertices in G, and s the start vertex. for(each vertex v){ currentDistance(s-v) = ∞; predecessor(v) = undefined; } currentDistance(s-s) = 0; T = V; while(T ){ v = a vertex in T with minimal currentDistance from s; T = T – {v}; for(each vertex u adjacent to v and in T){ if(currentDistance(s-u) > currentDistance(s-v) + weight(edge(vu)){ currentDistance(s-u) = currentDistance(s-v) + weight(edge(vu)); predecessor(u) = v; } For each vertex, the algorithm keeps track of its current distance from the starting vertex and the predecessor on the current path How can we implement Dijkstra’s algorithm? What is the time complexity of the algorithm?
22
Example 22 Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is: Passinitially 123456 weight Predecessor Active Vertex B A B0 C D E F
23
Example 23 Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is: Passinitially 123456 weight Predecessor Active Vertex BACDEF A 3### 3B B0 0- C 54 4A D 6 6C E 88 8C F 119###9E
24
Review Questions 24 Use the graph Gc shown above to trace the execution of Dijkstra's algorithm as it solves the shortest path problem starting from vertex a. Dijkstra's algorithm works as long as there are no negative edge weights. Given a graph that contains negative edge weights, we might be tempted to eliminate the negative weights by adding a constant weight to all of the edges. Explain why this does not work. Dijkstra's algorithm can be modified to deal with negative edge weights (but not negative cost cycles). Implement this modified algorithm.
25
Minimum Spanning Tree 25 What is a Minimum Spanning Tree. Constructing Minimum Spanning Trees. What is a Minimum-Cost Spanning Tree. Applications of Minimum Cost Spanning Trees. Prim’s Algorithm. Example. Implementation. Kruskal’s algorithm. Example. Implementation. Review Questions.
26
What is a Spanning Tree. 26 Let G = (V, E) be a simple, connected, undirected graph that is not edge- weighted. A spanning tree of G is a free tree (i.e., a tree with no root) with | V | - 1 edges that connects all the vertices of the graph. Thus a spanning tree for G is a graph, T = (V’, E’) with the following properties: V’ = V T is connected T is acyclic. A spanning tree is called a tree because every acyclic undirected graph can be viewed as a general, unordered tree. Because the edges are undirected, any vertex may be chosen to serve as the root of the tree.
27
Constructing Spanning Trees Any traversal of a connected, undirected graph visits all the vertices in that graph. The set of edges which are traversed during a traversal forms a spanning tree. For example, Fig:(b) shows the spanning tree obtained from a breadth-first traversal starting at vertex b. Similarly, Fig:(c) shows the spanning tree obtained from a depth-first traversal starting at vertex c. (a) Graph G (b) Breadth-first spanning tree of G rooted at b (c) Depth-first spanning tree of G rooted at c 27
28
What is a Minimum-Cost Spanning Tree For an edge-weighted, connected, undirected graph, G, the total cost of G is the sum of the weights on all its edges. A minimum-cost spanning tree for G is a spanning tree of G that has the least total cost. Example: The graph Has 16 spanning trees. Some are: The graph has two minimum-cost spanning trees, each with a cost of 6: 28
29
Applications of Minimum-Cost Spanning Trees Minimum-cost spanning trees have many applications. Some are: Building cable networks that join n locations with minimum cost. Building a road network that joins n cities with minimum cost. Obtaining an independent set of circuit equations for an electrical network. In pattern recognition minimal spanning trees can be used to find noisy pixels. 29
30
Prim’s Algorithm 30 Prim’s algorithm finds a minimum cost spanning tree by selecting edges from the graph one-by-one as follows: It starts with a tree, T, consisting of the starting vertex, x. Then, it adds the shortest edge emanating from x that connects T to the rest of the graph. It then moves to the added vertex and repeats the process. Consider a graph G=(V, E); Let T be a tree consisting of only the starting vertex x; while (T has fewer than IVI vertices) { find a smallest edge connecting T to G-T; add it to T; } How can we implement Prim’s algorithm? What is the time complexity of the algorithm?
31
Example Trace Prim’s algorithm starting at vertex a: The resulting minimum-cost spanning tree is: 31
32
Kruskal's Algorithm. 32 Kruskal’s algorithm also finds the minimum cost spanning tree of a graph by adding edges one-by-one. enqueue edges of G in a queue in increasing order of cost. T = ; while(queue is not empty){ dequeue an edge e; if(e does not create a cycle with edges in T) add e to T; } return T; How can we implement Kruskal’s algorithm? What is the time complexity of the algorithm?
33
Example for Kruskal’s Algorithm. Trace Kruskal's algorithm in finding a minimum-cost spanning tree for the undirected, weighted graph given below: The minimum cost is 33 edge weight Insertion status Insertion order
34
Example for Kruskal’s Algorithm. Trace Kruskal's algorithm in finding a minimum-cost spanning tree for the undirected, weighted graph given below: The minimum cost is: 24 34
35
Prim’s and Kruskal’s Algorithms Note: It is not necessary that Prim's and Kruskal's algorithm generate the same minimum-cost spanning tree. For example for the graph: Kruskal's algorithm (that imposes an ordering on edges with equal weights) results in the following minimum cost spanning tree: The same tree is generated by Prim's algorithm if the start vertex is any of: A, B, or D; however if the start vertex is C the minimum cost spanning tree is: 35
36
Review Questions 1.Find the breadth-first spanning tree and depth-first spanning tree of the graph GA shown above. 2.For the graph GB shown above, trace the execution of Prim's algorithm as it finds the minimum-cost spanning tree of the graph starting from vertex a. 3.Repeat question 2 above using Kruskal's algorithm. GBGB 36
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.