Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.

Slides:



Advertisements
Similar presentations
Chapter 5: Tree Constructions
Advertisements

Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Data Structures Using C++
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 8, Part I Graph Algorithms.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
Data Structures Using C++
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Algoritma Traversal Graph. Graph Traversal Algorithms In general, graphs do not have a vertex, like a root, that initiates unique paths to each of the.
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graph & BFS.
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.
Chapter 9: Graphs Summary Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Lydia Sinapova, Simpson College.
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.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 25 Graph.
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,
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
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.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
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 1 Graph Categories Strong Components Example of Digraph
Graphs Upon completion you will be able to:
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
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.
November 22, Algorithms and Data Structures Lecture XII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Graph Search Applications, Minimum Spanning Tree
CSC317 Graph algorithms Why bother?
C.Eng 213 Data Structures Graphs Fall Section 3.
Short paths and spanning trees
Graphs Graph transversals.
Graphs Chapter 11 Objectives Upon completion you will be able to:
CS 583 Analysis of Algorithms
Podcast Ch25d Title: Minimum Path Algorithm
Chapter 11 Graphs.
Podcast Ch25c Title: Shortest Path Algorithm
Chapter 16 1 – Graphs Graph Categories Strong Components
CSE 417: Algorithms and Computational Complexity
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Podcast Ch24b Title: Strongly Connected Components
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 .
Presentation transcript:

Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Possible topological sort: R37 R137 R51 R63 R53 R151 R173 R263

Running Time for Topological Sort The topological sort uses the algorithm for dfs(), so its running time is also O(V+E), where V is the number of vertices in the graph and E is the number of edges. The topological sort uses the algorithm for dfs(), so its running time is also O(V+E), where V is the number of vertices in the graph and E is the number of edges.

Strongly Connected Components Any graph can be partitioned into a unique set of strong components. Any graph can be partitioned into a unique set of strong components.

Strongly Connected Components (continued) The algorithm for finding the strong components of a directed graph G uses the transpose of the graph. The algorithm for finding the strong components of a directed graph G uses the transpose of the graph. The transpose G T has the same set of vertices V as graph G but a new edge set consisting of the edges of G but with the opposite direction. The transpose G T has the same set of vertices V as graph G but a new edge set consisting of the edges of G but with the opposite direction.

Strongly Connected Components (continued) Execute the depth-first search dfs() for the graph G which creates the list dfsList consisting of the vertices in G in the reverse order of their finishing times. Execute the depth-first search dfs() for the graph G which creates the list dfsList consisting of the vertices in G in the reverse order of their finishing times. Generate the transpose graph G T. Generate the transpose graph G T. Using the order of vertices in dfsList, make repeated calls to dfs() for vertices in G T. The list returned by each call is a strongly connected component of G. Using the order of vertices in dfsList, make repeated calls to dfs() for vertices in G T. The list returned by each call is a strongly connected component of G.

Strongly Connected Components (continued)

dfsList: [A, B, C, E, D, G, F] Using the order of vertices in dfsList, make successive calls to dfs() for graph G T Vertex A: dfs(A) returns the list [A, C, B] of vertices reachable from A in G T. Vertex E: The next unvisited vertex in dfsList is E. Calling dfs(E) returns the list [E]. Vertex D: The next unvisited vertex in dfsList is D; dfs(D) returns the list [D, F, G] whose elements form the last strongly connected component..

strongComponents() // find the strong components of the graph; // each element of component is a LinkedList // of the elements in a strong component public static void strongComponents(DiGraph g, ArrayList > component) { T currVertex = null; // list of vertices visited by dfs() for graph g LinkedList dfsList = new LinkedList (); // list of vertices visited by dfsVisit() // for g transpose LinkedList dfsGTList = null; // used to scan dfsList Iterator gIter; // transpose of the graph DiGraph gt = null;

strongComponents() (continued) // clear the return vector component.clear(); // execute depth-first traversal of g dfs(g, dfsList); // compute gt gt = transpose(g); // initialize all vertices in gt to WHITE (unvisited) gt.colorWhite();

strongComponents() (continued) // call dfsVisit() for gt from vertices in dfsList gIter = dfsList.iterator(); while(gIter.hasNext()) { currVertex = gIter.next(); // call dfsVisit() only if vertex // has not been visited if (gt.getColor(currVertex) == VertexColor.WHITE) {

strongComponents() (concluded) // create a new LinkedList to hold // next strong component dfsGTList = new LinkedList (); // do dfsVisit() in gt for starting // vertex currVertex dfsVisit(gt, currVertex, dfsGTList, false); // add strong component to the ArrayList component.add(dfsGTList); }

Running Time of strongComponents() Recall that the depth-first search has running time O(V+E), and the computation for G T is also O(V+E). It follows that the running time for the algorithm to compute the strong components is O(V+E). Recall that the depth-first search has running time O(V+E), and the computation for G T is also O(V+E). It follows that the running time for the algorithm to compute the strong components is O(V+E).

Graph Optimization Algorithms Dijkstra's algorithm computes minimum path weight from a specified vertex to all other vertices in the graph. Dijkstra's algorithm computes minimum path weight from a specified vertex to all other vertices in the graph. The breadth ‑ first search can be used to find the shortest path from a specific vertex to all the other vertices in the graph. The breadth ‑ first search can be used to find the shortest path from a specific vertex to all the other vertices in the graph. Minimum spanning tree for a connected, undirected graph is the set of edges that connect all vertices in the graph with the smallest total weight. Minimum spanning tree for a connected, undirected graph is the set of edges that connect all vertices in the graph with the smallest total weight.

The Shortest Path Algorithm The breadth-first search can be modified to find shortest paths. Begin at vertex sVertex and visit its neighbors (path length 1) followed by vertices with successively larger path lengths. The algorithm fans out from sVertex along paths of adjacent vertices until it visits all vertices reachable from sVertex. The breadth-first search can be modified to find shortest paths. Begin at vertex sVertex and visit its neighbors (path length 1) followed by vertices with successively larger path lengths. The algorithm fans out from sVertex along paths of adjacent vertices until it visits all vertices reachable from sVertex.

The Shortest Path Algorithm (continued) Each vertex must maintain a record of its parent and its path length from sVertex. The DiGraph class provides methods that allow the programmer to associate two fields of information with a vertex. Each vertex must maintain a record of its parent and its path length from sVertex. The DiGraph class provides methods that allow the programmer to associate two fields of information with a vertex. One field identifies the parent of a vertex and the other field is an integer dataValue associated with the vertex. The method initData() assigns a representation for  to each dataValue field of the graph vertices. One field identifies the parent of a vertex and the other field is an integer dataValue associated with the vertex. The method initData() assigns a representation for  to each dataValue field of the graph vertices.

The Shortest Path Algorithm (continued)

A breadth ‑ first search visit to a vertex defines a path of shortest length from the starting vertex. A breadth ‑ first search visit to a vertex defines a path of shortest length from the starting vertex. The parent field of the vertex determines the order of vertices from the starting vertex. The parent field of the vertex determines the order of vertices from the starting vertex.

Dijkstra's Minimum-Path Algorithm The minimum path problem is to determine a path of minimum weight from a starting vertex v s to each reachable vertex in the graph. The path may contain more vertices than the shortest path from v s. The minimum path problem is to determine a path of minimum weight from a starting vertex v s to each reachable vertex in the graph. The path may contain more vertices than the shortest path from v s.

Dijkstra's Minimum-Path Algorithm (continued) Three paths, A-B-E, A-C-E, and A-D-E, have path length 2, with weights 15, 17, and 13 respectively. The minimum path is A-C-D-E, with weight 11 but path length 3.

Dijkstra's Minimum-Path Algorithm (continued) Define the distance from every node to the starting node: Initially the distance is 0 to the starting node and infinity to other nodes. Define the distance from every node to the starting node: Initially the distance is 0 to the starting node and infinity to other nodes. Use a priority queue to store all the nodes with distance (the minimal distance node on the top). Use a priority queue to store all the nodes with distance (the minimal distance node on the top). Each step in the algorithm removes a node from the priority queue and update the distance to other nodes in the queue via this node. Each step in the algorithm removes a node from the priority queue and update the distance to other nodes in the queue via this node.

Dijkstra's Minimum-Path Algorithm (continued) Since no subsequent step could find a new path to the node with a smaller weight (because the weights are positive), we have found the minimum path to this node. Since no subsequent step could find a new path to the node with a smaller weight (because the weights are positive), we have found the minimum path to this node. Repeat this above step and the algorithm terminates whenever the priority queue becomes empty. Repeat this above step and the algorithm terminates whenever the priority queue becomes empty.

Dijkstra's Minimum-Path Algorithm (continued)

Running Time of Dijkstra's Algorithm Dijkstra's algorithm has running time O(V + E log 2 V). Dijkstra's algorithm has running time O(V + E log 2 V).

Minimum Path in Acyclic Graphs When the weighted digraph is acyclic, the problem of finding minimum paths is greatly simplified. When the weighted digraph is acyclic, the problem of finding minimum paths is greatly simplified. The depth-first search creates a list of vertices in topolocial order. dfsList : [v 0, v 1,...,v i,..., v n-1 ] The depth-first search creates a list of vertices in topolocial order. dfsList : [v 0, v 1,...,v i,..., v n-1 ] Assume v i is the starting vertex for the minimum-path problem. Vertices in the list v 0 to v i-1 are not reachable from vi. Assume v i is the starting vertex for the minimum-path problem. Vertices in the list v 0 to v i-1 are not reachable from vi.

Minimum Path in Acyclic Graphs (continued) After initializing the data value for all of the vertices to , set the data value for v i to 0 and its parent reference to v i. A scan of the vertices in dfsList will find that v 0 through v i-1 have value  and will not be considered. The algorithm discovers v i and iteratively scans the tail of dfsList, proceeding much like Dijkstra's algorithm. After initializing the data value for all of the vertices to , set the data value for v i to 0 and its parent reference to v i. A scan of the vertices in dfsList will find that v 0 through v i-1 have value  and will not be considered. The algorithm discovers v i and iteratively scans the tail of dfsList, proceeding much like Dijkstra's algorithm.

Minimum Path in Acyclic Graphs (continued) At a vertex v in the sequential scan of dfsList, its current data value is the minimum path weight from v i to v. For there to be a better path, there must be an unvisited vertex, v', reachable from v i that has an edge to v. This is not possible, since topological order guarantees that v' will come earlier in dfsList. At a vertex v in the sequential scan of dfsList, its current data value is the minimum path weight from v i to v. For there to be a better path, there must be an unvisited vertex, v', reachable from v i that has an edge to v. This is not possible, since topological order guarantees that v' will come earlier in dfsList.

Minimum Path in Acyclic Graphs (continued)

Running Time for Minimum Path in Acyclic Graphs The algorithm first creates a topological sort of the vertices with running time O(V+E). A loop visits all of the vertices in the graph once and examines edges that eminate from each vertex only once. Access to all of the vertices and edges has running time O(V+E) and so the total running time for the algorithm is O(V+E). The algorithm first creates a topological sort of the vertices with running time O(V+E). A loop visits all of the vertices in the graph once and examines edges that eminate from each vertex only once. Access to all of the vertices and edges has running time O(V+E) and so the total running time for the algorithm is O(V+E).

Minimum Spanning Tree A minimum spanning tree for a connected undirected graph is an acyclic set of edges that connect all the vertices of the graph with the smallest possible total weight. A minimum spanning tree for a connected undirected graph is an acyclic set of edges that connect all the vertices of the graph with the smallest possible total weight. A network connects hubs in a system. The minimum spanning tree links all of the nodes in the system with the least amount of cable. A network connects hubs in a system. The minimum spanning tree links all of the nodes in the system with the least amount of cable.

Minimum Spanning Tree (continued)

Prim's Algorithm The mechanics are very similar to the Dijkstra minimum-path algorithm. The mechanics are very similar to the Dijkstra minimum-path algorithm. The iterative process begins with any starting vertex and maintains two variables minSpanTreeSize and minSpanTreeWeight, which have initial values 0. The iterative process begins with any starting vertex and maintains two variables minSpanTreeSize and minSpanTreeWeight, which have initial values 0. Each step adds a new vertex to the spanning tree. It has an edge of minimal weight that connects the vertex to those already in the minimal spanning tree. Each step adds a new vertex to the spanning tree. It has an edge of minimal weight that connects the vertex to those already in the minimal spanning tree.

Prim's Algorithm (continued) Prim's algorithm uses a priority queue where elements store a vertex and the weight of an edge that can connect it to the tree. Prim's algorithm uses a priority queue where elements store a vertex and the weight of an edge that can connect it to the tree. When an object comes out of the priority queue, it defines a new edge that connects a vertex to the spanning tree. When an object comes out of the priority queue, it defines a new edge that connects a vertex to the spanning tree.

Prim's Algorithm (continued)

Running Time for Prim's Algorithm Like Dijkstra's algorithm, Prim's algorithm has running time O(V + E log 2 V). Like Dijkstra's algorithm, Prim's algorithm has running time O(V + E log 2 V).