+ All Categories
Home > Documents > Topological Sort

Topological Sort

Date post: 05-Jan-2016
Category:
Upload: jadon
View: 39 times
Download: 1 times
Share this document with a friend
Description:
Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological Sort. Possible topological sort: R37 R137 R51 R63 R53 R151 R173 R263. - PowerPoint PPT Presentation
38
Topological Sort Topological Sort Topological sort is the list of vertices in Topological sort is the list of vertices in the reverse order of their finishing times the reverse order of their finishing times (post-order) of the depth-first search. (post-order) of the depth-first search. sible topological sort: R37 R137 R51 R63 R53 R151 R173 R263 sible topological sort: R37 R137 R51 R63 R53 R151 R173 R263
Transcript
Page 1: Topological Sort

Topological SortTopological Sort Topological sort is the list of vertices in Topological sort is the list of vertices in

the reverse order of their finishing times the reverse order of their finishing times (post-order) of the depth-first search. (post-order) of the depth-first search.

Possible topological sort: R37 R137 R51 R63 R53 R151 R173 R263Possible topological sort: R37 R137 R51 R63 R53 R151 R173 R263

Page 2: Topological Sort

Running Time for Running Time for Topological SortTopological Sort

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

Page 3: Topological Sort

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

Page 4: Topological Sort

Strongly Connected Strongly Connected Components (continued)Components (continued)

The algorithm for finding the strong The algorithm for finding the strong components of a directed graph G components of a directed graph G uses the transpose of the graph.uses the transpose of the graph. The transpose GThe transpose GTT has the same set of has the same set of

vertices V as graph G but a new edge vertices V as graph G but a new edge set consisting of the edges of G but with set consisting of the edges of G but with the opposite direction. the opposite direction.

Page 5: Topological Sort

Strongly Connected Strongly Connected Components (continued)Components (continued)

Execute the depth-first search dfs() for Execute the depth-first search dfs() for the graph G which creates the list dfsList the graph G which creates the list dfsList consisting of the vertices in G in the consisting of the vertices in G in the reverse order of their finishing times.reverse order of their finishing times.

Generate the transpose graph GGenerate the transpose graph GTT.. Using the order of vertices in dfsList, Using the order of vertices in dfsList,

make repeated calls to dfs() for vertices make repeated calls to dfs() for vertices in Gin GTT. The list returned by each call is a . The list returned by each call is a strongly connected component of G.strongly connected component of G.

Page 6: Topological Sort

Strongly Connected Strongly Connected Components (continued)Components (continued)

Page 7: Topological Sort

Strongly Connected Strongly Connected Components (continued)Components (continued)

dfsList: [A, B, C, E, D, G, F]dfsList: [A, B, C, E, D, G, F]

Using the order of vertices in dfsList, make successiveUsing the order of vertices in dfsList, make successivecalls to dfs() for graph Gcalls to dfs() for graph GTT

Vertex A: dfs(A) returns the list [A, C, B] of vertices reachableVertex A: dfs(A) returns the list [A, C, B] of vertices reachablefrom A in Gfrom A in GTT. .

Vertex E: The next unvisited vertex in dfsList is E. Calling dfs(E)Vertex E: The next unvisited vertex in dfsList is E. Calling dfs(E)returns the list [E]. returns the list [E].

Vertex D: The next unvisited vertex in dfsList is D; dfs(D) returnsVertex D: The next unvisited vertex in dfsList is D; dfs(D) returnsthe list [D, F, G] whose elements form the lastthe list [D, F, G] whose elements form the laststrongly connected component.. strongly connected component..

Page 8: Topological Sort

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

Page 9: Topological Sort

strongComponents()strongComponents()(continued)(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();

Page 10: Topological Sort

strongComponents()strongComponents()(continued)(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) {

Page 11: Topological Sort

strongComponents()strongComponents()(concluded)(concluded)

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

Page 12: Topological Sort

Running Time of Running Time of strongComponents()strongComponents()

Recall that the depth-first search has Recall that the depth-first search has running time O(V+E), and the running time O(V+E), and the computation for Gcomputation for GTT is also O(V+E). It is also O(V+E). It follows that the running time for the follows that the running time for the algorithm to compute the strong algorithm to compute the strong components is O(V+E).components is O(V+E).

Page 13: Topological Sort

Graph Optimization Graph Optimization AlgorithmsAlgorithms

Dijkstra's algorithm computes minimum Dijkstra's algorithm computes minimum path weight from a specified vertex topath weight from a specified vertex toall other vertices in the graph.all other vertices in the graph.

The breadth‑first search can be used to find The breadth‑first search can be used to find the shortest path from a specific vertex to the shortest path from a specific vertex to all the other vertices in the graph.all the other vertices in the graph.

Minimum spanning tree for a connected, Minimum spanning tree for a connected, undirected graph is the set of edges that undirected graph is the set of edges that connect all vertices in the graph with the connect all vertices in the graph with the smallest total weight. smallest total weight.

Page 14: Topological Sort

The Shortest Path AlgorithmThe Shortest Path Algorithm

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

Page 15: Topological Sort

The Shortest Path Algorithm The Shortest Path Algorithm (continued)(continued)

Each vertex must maintain a record of its Each vertex must maintain a record of its parent and its path length from sVertex. parent and its path length from sVertex. The DiGraph class provides methods that The DiGraph class provides methods that allow the programmer to associate two allow the programmer to associate two fields of information with a vertex.fields of information with a vertex. One field identifies the parent of a vertex and One field identifies the parent of a vertex and

the other field is an integer dataValue the other field is an integer dataValue associated with the vertex. The method associated with the vertex. The method initData() assigns a representation for initData() assigns a representation for to to each dataValue field of the graph vertices. each dataValue field of the graph vertices.

Page 16: Topological Sort

The Shortest Path Algorithm The Shortest Path Algorithm (continued)(continued)

class DiGraph<T> (dataValue and parent methods) ds.util

int getData(T v) Returns the integer data value associated with vertex v. If v is not a graph vertex, throws IllegalArgumentException.

T getParent(T v) Returns the parent of vertex v. if v is not a graph vertex, throws IllegalArgumentException.

int setData(T v, int value) Sets the integer data value associated with vertex v and returns the previous value. If v is not a graph vertex, throws IllegalArgumentException.

T setParent(T v, T p) Assigns the parent of vertex v to be p and returns the previous parent. If v or p is not a graph vertex, throws IllegalArgumentException.

void initData() Assign the dataValue field of each vertex to .

Page 17: Topological Sort

The Shortest Path Algorithm The Shortest Path Algorithm (continued)(continued)

A breadth‑first search visit to a A breadth‑first search visit to a vertex defines a path of shortest vertex defines a path of shortest length from the starting vertex.length from the starting vertex.

The parent field of the vertex The parent field of the vertex determines the order of vertices determines the order of vertices from the starting vertex.from the starting vertex.

Page 18: Topological Sort

Dijkstra's Minimum-Path Dijkstra's Minimum-Path AlgorithmAlgorithm

The minimum path problem is to The minimum path problem is to determine a path of minimum weight determine a path of minimum weight from a starting vertex vfrom a starting vertex vss to each to each reachable vertex in the graph. The reachable vertex in the graph. The path may contain more vertices than path may contain more vertices than the shortest path from vthe shortest path from vss..

Page 19: Topological Sort

Dijkstra's Minimum-Path Dijkstra's Minimum-Path Algorithm (continued)Algorithm (continued)

Three paths, A-B-E, A-C-E, and A-D-E, have path length 2, with weightsThree paths, A-B-E, A-C-E, and A-D-E, have path length 2, with weights15, 17, and 13 respectively. The minimum path is A-C-D-E, with weight 1115, 17, and 13 respectively. The minimum path is A-C-D-E, with weight 11but path length 3. but path length 3.

Page 20: Topological Sort

Dijkstra's Minimum-Path Dijkstra's Minimum-Path Algorithm (continued)Algorithm (continued)

Define the distance from every node to the Define the distance from every node to the starting node: Initially the distance is 0 to starting node: Initially the distance is 0 to the starting node and infinity to other nodes. the starting node and infinity to other nodes.

Use a priority queue to store all the nodes Use a priority queue to store all the nodes with distance (the minimal distance node on with distance (the minimal distance node on the top).the top).

Each step in the algorithm removes a node Each step in the algorithm removes a node from the priority queue and update the from the priority queue and update the distance to other nodes in the queue via this distance to other nodes in the queue via this node.node.

Page 21: Topological Sort

Dijkstra's Minimum-Path Dijkstra's Minimum-Path Algorithm (continued)Algorithm (continued)

Since no subsequent step could find a new Since no subsequent step could find a new path to the node with a smaller weight path to the node with a smaller weight (because the weights are positive), we (because the weights are positive), we have found the minimum path to this have found the minimum path to this node. node.

Repeat this above step and the algorithm Repeat this above step and the algorithm terminates whenever the priority queue terminates whenever the priority queue becomes empty.becomes empty.

Page 22: Topological Sort

Dijkstra's Minimum-Path Dijkstra's Minimum-Path Algorithm (continued)Algorithm (continued)

M I(E,15) M I(D ,8)M I(D ,6)

(d ) P o p M I(C ,4). P u s h M I(D ,6) in to th e p rio rity q u e u e

M I(E,15) M I(E,11)

(f) P o p M I(D ,8) (g ) P o p M I(E,11)

M I(E,15)M I(E,15) M I(D ,8)M I(E,11)

(e ) P o p M I(D ,6). P u s h M I(E,11) in to th e p rio rity q u e u e

Page 23: Topological Sort

Running Time of Dijkstra's Running Time of Dijkstra's AlgorithmAlgorithm

Dijkstra's algorithm has running time Dijkstra's algorithm has running time O(V + E logO(V + E log22V).V).

Page 24: Topological Sort

Minimum Path in AcyclicMinimum Path in AcyclicGraphsGraphs

When the weighted digraph is When the weighted digraph is acyclic, the problem of finding acyclic, the problem of finding minimum paths is greatly simplified.minimum paths is greatly simplified. The depth-first search creates a list of The depth-first search creates a list of

vertices in topolocial order.vertices in topolocial order.dfsList : [vdfsList : [v00, v, v11, . . .,v, . . .,vii, . . ., v, . . ., vn-1n-1]]

Assume vAssume vii is the starting vertex for the is the starting vertex for the minimum-path problem. Vertices in the minimum-path problem. Vertices in the list vlist v00 to v to vi-1i-1 are not reachable from vi. are not reachable from vi.

Page 25: Topological Sort

Minimum Path in AcyclicMinimum Path in AcyclicGraphs (continued)Graphs (continued)

After initializing the data value for all After initializing the data value for all of the vertices to of the vertices to , set the data value , set the data value for vfor vii to 0 and its parent reference to to 0 and its parent reference to vvii. A scan of the vertices in dfsList will . A scan of the vertices in dfsList will find that vfind that v00 through v through vi-1i-1 have value have value and will not be considered. The and will not be considered. The algorithm discovers valgorithm discovers vii and iteratively and iteratively scans the tail of dfsList, proceeding scans the tail of dfsList, proceeding much like Dijkstra's algorithm.much like Dijkstra's algorithm.

Page 26: Topological Sort

Minimum Path in AcyclicMinimum Path in AcyclicGraphs (continued)Graphs (continued)

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

Page 27: Topological Sort

Minimum Path in AcyclicMinimum Path in AcyclicGraphs (continued)Graphs (continued)

Page 28: Topological Sort

Running Time for MinimumRunning Time for MinimumPath in Acyclic GraphsPath in Acyclic Graphs

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

Page 29: Topological Sort

Minimum Spanning TreeMinimum Spanning Tree

A minimum spanning tree for a A minimum spanning tree for a connected undirected graph is an connected undirected graph is an acyclic set of edges that connect all acyclic set of edges that connect all the vertices of the graph with the the vertices of the graph with the smallest possible total weight.smallest possible total weight. A network connects hubs in a system. A network connects hubs in a system.

The minimum spanning tree links all of The minimum spanning tree links all of the nodes in the system with the least the nodes in the system with the least amount of cable. amount of cable.

Page 30: Topological Sort

Minimum Spanning Tree Minimum Spanning Tree (continued)(continued)

Page 31: Topological Sort

Prim's AlgorithmPrim's Algorithm The mechanics are very similar to the The mechanics are very similar to the

Dijkstra minimum-path algorithm.Dijkstra minimum-path algorithm. The iterative process begins with any The iterative process begins with any

starting vertex and maintains two variables starting vertex and maintains two variables minSpanTreeSize and minSpanTreeWeight, minSpanTreeSize and minSpanTreeWeight, which have initial values 0.which have initial values 0.

Each step adds a new vertex to the Each step adds a new vertex to the spanning tree. It has an edge of minimal spanning tree. It has an edge of minimal weight that connects the vertex to those weight that connects the vertex to those already in the minimal spanning tree. already in the minimal spanning tree.

Page 32: Topological Sort

Prim's Algorithm (continued)Prim's Algorithm (continued)

Prim's algorithm uses a priority Prim's algorithm uses a priority queue where elements store a vertex queue where elements store a vertex and the weight of an edge that can and the weight of an edge that can connect it to the tree.connect it to the tree.

When an object comes out of the When an object comes out of the priority queue, it defines a new edge priority queue, it defines a new edge that connects a vertex to the that connects a vertex to the spanning tree.spanning tree.

Page 33: Topological Sort

Prim's Algorithm (continued)Prim's Algorithm (continued)

Page 34: Topological Sort

Prim's Algorithm (continued)Prim's Algorithm (continued)

Page 35: Topological Sort

Prim's Algorithm (continued)Prim's Algorithm (continued)

Page 36: Topological Sort

Prim's Algorithm (continued)Prim's Algorithm (continued)

Page 37: Topological Sort

Prim's Algorithm (continued)Prim's Algorithm (continued)

Page 38: Topological Sort

Running Time for Prim's Running Time for Prim's AlgorithmAlgorithm

Like Dijkstra's algorithm, Prim's Like Dijkstra's algorithm, Prim's algorithm has running time O(V + algorithm has running time O(V +  E log E log22V).V).


Recommended