Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.

Slides:



Advertisements
Similar presentations
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Advertisements

Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph Traversals Reading Material: Chapter 9. Graph Traversals Some applications require visiting every vertex in the graph exactly once. The application.
Testing for Connectedness and Cycles
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
1 Minimum Spanning Trees (MSTs) and Minimum Cost Spanning Trees (MCSTs) What is a Minimum Spanning Tree? Constructing Minimum Spanning Trees. What is a.
1 Testing for Connectedness and Cycles Connectedness of Undirected Graphs Implementation of Connectedness detection Algorithm for Undirected Graphs. Implementation.
Introduction to Graphs
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Shortest Path Algorithm What is the Shortest Path Problem? Is the shortest path problem well defined? The Dijkstra's Algorithm for Shortest Path Problem.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Testing for Connectedness & Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong Connectedness.
Testing for Connectedness and Cycles Connectedness of an Undirected Graph Implementation of Connectedness detection Algorithm. Implementation of Strong.
Copyright © Cengage Learning. All rights reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms Shortest-Path.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
COSC 2007 Data Structures II Chapter 14 Graphs III.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Spanning Trees Introduction to Spanning Trees AQR MRS. BANKS Original Source: Prof. Roger Crawfis from Ohio State University.
Graph Traversal BFS & DFS. Review of tree traversal methods Pre-order traversal In-order traversal Post-order traversal Level traversal a bc d e f g hi.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
Kruskal’s and Dijkstra’s Algorithm.  Kruskal's algorithm is an algorithm in graph theory that finds a minimum spanning tree for a connected weighted.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
Minimum Spanning Tree What is a Minimum Spanning Tree.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graph Search Applications, Minimum Spanning Tree
Graphs Chapter 20.
Minimum Spanning Trees
Minimum Spanning Tree What is a Minimum Spanning Tree.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Shortest Path Algorithm
Minimum Spanning Tree Chapter 13.6.
C.Eng 213 Data Structures Graphs Fall Section 3.
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Spanning Tree A spanning tree is any tree that consists solely of edges in G and that includes all the vertices in G. Example.
Graph Algorithm.
Minimum Spanning Trees
Graphs Chapter 13.
Testing for Connectedness and Cycles
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373 Data Structures and Algorithms
Minimum Spanning Tree Section 7.3: Examples {1,2,3,4}
CSE 373: Data Structures and Algorithms
Chapter 16 1 – Graphs Graph Categories Strong Components
Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV
Shortest Path Algorithm
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Shortest Path Algorithm
Presentation transcript:

Unit 11 Graphs (2) King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department

 “Data Structures and Algorithms in Java”, 3rd Edition, Adam Drozdek, Cengage Learning, ISBN  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

Outline  Graph Connectedness and Cycles  Dijkstra’s Shortest Path Algorithm  Minimum Spanning Tree Algorithms  Prim’s Algorithm  Kruskal’s Algorithm 3

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

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}}

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.

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)

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 …..

Example

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

Example

Example

Pop Quiz: Run SCC Algorithm on

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

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

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

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!

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.

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

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.

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?

Example 22 Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is: Passinitially weight Predecessor Active Vertex B A  B0 C  D  E  F 

Example 23 Tracing Dijkstra’s algorithm starting at vertex B: The resulting vertex-weighted graph is: Passinitially weight Predecessor Active Vertex BACDEF A  3### 3B B0 0- C  54 4A D  6 6C E  88 8C F  119###9E

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.

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.

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.

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

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

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

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?

Example Trace Prim’s algorithm starting at vertex a: The resulting minimum-cost spanning tree is: 31

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?

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

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

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

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