Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV

2 Topological Sort A directed acyclic graph (DAG) is a directed graph without cycles. A topological sort of a DAG = (V, E) is a linear ordering of vertices in V consistent with the partial order a < b if (a, b) is in E. In other words, each vertex in a topological sort must precede all its descendants in the DAG and must follow all its ancestors. Another way to look at it: A topological sort lines up all the vertices in the DAG so that all the edges point from left to right.

3 Algorithm for Topological sort
Topological-Sort(G) Call DFS(G) to compute the finish times v.finish for each vertex in v. As each vertex is finished, insert it into the front of a linked list. Return the linked list of vertices. Because each vertex is finished after all its descendants are finished, each vertex precedes all its descendants in the list. Running time = Running time of DFS + time to insert into list = Q(V+E) + Q(V) = Q(V + E)

4 Example: Professor Bumstead gets dressed
Professor Bumstead must put on certain items before others (e.g. socks before shoes). socks watch undershorts shoes pants shirt We will work out the topological sort in class belt tie jacket

5 Connected and Strongly Connected components
A connected component of a graph is a maximal set of vertices such that for any two vertices, a and b, in the set, there is a path from a to b or from b to a. In other words: two vertices are in the same connected component if there is a path from one to the other. A strongly connected component of a graph is a maximal set of vertices such that for any two vertices, a and b, in the set, there is a path from a to b and from b to a. In other words: In a strongly connected component there is a path from every member of the set to every other member of the set. Importance: Decomposing a graph into its strongly connected components is the first step in many graph algorithms.

6 Algorithm for strongly connected components
Definition: The transpose of G, written GT, is a graph with the same vertices as G in which the directions of the edges have been reversed. Strongly-Connected-Components(G) Call DFS(G) to compute v.finish for each vertex in G. Call Modified-DFS(GT), where the main loop of Modified-DFS(GT) processes vertices in order of decreasing finish[v]. Each tree in the depth-first forest of Modified-DFS(GT) is a strongly connected component of G. Running time = running time of DFS = Q(V + E)

7 Example a b c d e f g h

8 Minimizing Distance 10 5 12 1 8 1 16 2 2 14 How can we wire together a group of towns using the minimum total length of cable so that each town is connected to the network?

9 The Minimum Spanning Tree Problem
Given: A connected graph, G = <V, E>, such that each edge, is assigned a non-negative length (or weight). Problem: Find a subset T of E such that <V, T> remains connected, and the sum of the lengths of the edges is as small as possible subject to the above. Note: T is always acyclic. (Why?)

10 A Greedy Solution At each step, make a greedy choice (i.e. the choice that seems best at the time). What is greedy choice? Choose the shortest edge, x, such that our tree remains acyclic (i.e. that connects 2 components that were not previously connected).

11 Kruskal's Algorithm Kruskal(G, w) sort the edges by increasing weight
for each edge e on the sorted list do if e does not form a cycle with the edges already taken then include e in the spanning tree else discard e return resulting edge set

12 Is Greed Always Good? A slightly different problem:
Given: A connected graph G = <V, E> such that each edge is assigned a non-negative length. Problem: Find a subset T of E such that the edges of T form a closed path that includes every node and have the smallest possible total weight. 1000 2 1 1 2 1


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 35 Graphs IV"

Similar presentations


Ads by Google