Topological Sort (topological order)

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Graph Theory Arnold Mesa. Basic Concepts n A graph G = (V,E) is defined by a set of vertices and edges v3 v1 v2Vertex (v1) Edge (e1) A Graph with 3 vertices.
10.4 Spanning Trees. Def Def: Let G be a simple graph. A spanning tree of G is a subgraph of G that is a tree containing every vertex of G See handout.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Chapter 3 The Greedy Method 3.
Chapter 23 Minimum Spanning Trees
Graph Algorithms: Minimum Spanning Tree We are given a weighted, undirected graph G = (V, E), with weight function w:
3 -1 Chapter 3 The Greedy Method 3 -2 The greedy method Suppose that a problem can be solved by a sequence of decisions. The greedy method has that each.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Design and Analysis of Algorithms Minimum Spanning trees
Minimum Spanning Trees. Subgraph A graph G is a subgraph of graph H if –The vertices of G are a subset of the vertices of H, and –The edges of G are a.
1.1 Data Structure and Algorithm Lecture 13 Minimum Spanning Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Minimum Spanning Trees.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 223 – Advanced Data Structures Graph Algorithms: Minimum.
© The McGraw-Hill Companies, Inc., Chapter 3 The Greedy Method.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
9/23/2015ACM-ICPC1 Maximum Flow Problem Source t fg e 42 Target (sink) flow capacity b s c a de 2/3/ actual flow 1/2/ 1/ 2/ 1.Actual flow  capacity.
10/13/2015IT 328, review graph algorithms1 Topological Sort ( topological order ) Let G = (V, E) be a directed graph with |V| = n. 1.{ v 1, v 2,.... v.
Dijkstra’s Algorithm. Announcements Assignment #2 Due Tonight Exams Graded Assignment #3 Posted.
Module 5 – Networks and Decision Mathematics Chapter 23 – Undirected Graphs.
EMIS 8374 Optimal Trees updated 25 April slide 1 Minimum Spanning Tree (MST) Input –A (simple) graph G = (V,E) –Edge cost c ij for each edge e 
Fundamental Data Structures and Algorithms (Spring ’05) Recitation Notes: Graphs Slides prepared by Uri Dekel, Based on recitation.
Minimum Spanning Trees CS 146 Prof. Sin-Min Lee Regina Wang.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 14, 2014.
Midwestern State University Minimum Spanning Trees Definition of MST Generic MST algorithm Kruskal's algorithm Prim's algorithm 1.
Kruskal’s Algorithm for Computing MSTs Section 9.2.
Graph Search Applications, Minimum Spanning Tree
Minimum Spanning Trees
COMP108 Algorithmic Foundations Greedy methods
Greedy Technique.
Minimum Spanning Tree Chapter 13.6.
Graph theory Definitions Trees, cycles, directed graphs.
Lectures on Network Flows
Minimum Spanning Trees and Shortest Paths
CISC 235: Topic 10 Graph Algorithms.
Introduction to Graphs
Graphs: As a mathematics branch
Bipartite Matching and Other Graph Algorithms
CS120 Graphs.
Topological Sort (topological order)
Short paths and spanning trees
More Graph Algorithms.
Autumn 2016 Lecture 11 Minimum Spanning Trees (Part II)
Graph Algorithm.
Minimum-Cost Spanning Tree
Maximum Flow Problem flow capacity Actual flow  capacity
Minimum Spanning Tree.
Minimum Spanning Trees
Connected Components Minimum Spanning Tree
Graph Algorithm.
Graph Theory.
Graph.
CSE 373 Data Structures and Algorithms
Spanning Trees.
Minimum-Cost Spanning Tree
Minimum-Cost Spanning Tree
Autumn 2015 Lecture 10 Minimum Spanning Trees
Graph Searching.
CSCI2100 Data Structures Tutorial
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
CSE 373: Data Structures and Algorithms
Text Book: Introduction to algorithms By C L R S
Autumn 2016 Lecture 10 Minimum Spanning Trees
CSE 417: Algorithms and Computational Complexity
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
CSE 373: Data Structures and Algorithms
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Minimum-Cost Spanning Tree
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 order) Let G = (V, E) be a directed graph with |V| = n. A Topological Sort is a sequence v1, v2,.... vn, such that { v1, v2,.... vn } = V, and for every i and j with 1  i < j  n, (vj , vi)  E. g a f h e c b d h g e b There are more than one such order in this graph!! d c a f 11/12/2018 IT 328, review graph algorithms

Algorithm for finding Topological Sort x Principle: vertex with in-degree 0 should be listed first. x y z a Algorithm: y b Input G=(V,E) Repeat the following steps until no more vertex left: Find a vertex v with in-degree 0; If can’t find such v and V is not empty then stop the algorithm; this graph is not acyclic 2. Remove v from V and update E 3. Add v to the end of the sort 11/12/2018 IT 328, review graph algorithms

Topological Sort (topological order) of an acyclic graph b d h g e b d c a f 11/12/2018 IT 328, review graph algorithms

IT 328, review graph algorithms Finding the shortest path between two vertices Starting Vertex Starting Vertex Unweighted Graphs  Weighted Graphs 2 5 1 2 2 3 3 5 1 1 1 5 3 3 7 7 7 2 2 1 9 10 12 8 8 2 Both are using the greedy algorithm. w w Final Vertex Final Vertex 11/12/2018 IT 328, review graph algorithms

IT 328, review graph algorithms Construct a shortest path Map for weighted graph : decided Starting Vertex  0,-1 2 5   1 1 2 2,0 5,0 3,1 1 5   3 4 7 3,1 7,1 1 9  5 10,2 8,4 w-1,y n w,x Final Vertex 11/12/2018 IT 328, review graph algorithms

A spanning is a walk such that every vertex is included Minimum Spanning Tree of an undirected graph: The lowest cost to connect all vertices Since a cycle is not necessary, such a connection must be a tree. 2 1 2 1 10 4 3 2 3 4 7 5 8 4 5 6 1 6 7 11/12/2018 IT 328, review graph algorithms

grow the minimum spanning tree from a root Prim’s algorithm: grow the minimum spanning tree from a root 2 1 2 1 10 4 3 2 3 4 7 5 8 4 5 6 1 6 7 2 1 2 1 2 3 4 5 4 6 1 6 7 11/12/2018 IT 328, review graph algorithms

IT 328, review graph algorithms Prim’s algorithm: grow the minimum spanning tree from a different root 2 1 2 1 10 4 3 2 3 4 7 5 8 4 5 6 1 6 7 2 1 2 1 2 3 4 5 4 6 1 6 7 11/12/2018 IT 328, review graph algorithms

IT 328, review graph algorithms Kruskal’s Algorithm: construct the minimum spanning from edges 2 1 2 1 10 4 3 2 3 4 7 5 8 4 5 6 1 6 7 2 1 2 1 2 3 4 5 4 6 1 6 7 11/12/2018 IT 328, review graph algorithms

Maximum Flow Problem flow capacity Actual flow  capacity The source node can produce flow as much as possible For every node other than s and t, flow out  flow in Source s actual flow 3/ 4 2/ 2 1/ 1 a b 2/ 3 2/ 2 1/ 2 our textbook says = d c e e The target node’s flow-in is called the flow of the graph f g A node may have different ways to dispatch its flow-out . 1/ 2 2/ 3 t What is the maximum flow of the graph? Target (sink) 11/12/2018 IT 279

What makes the maximum flow problem more difficult than the shortest path problem? Source s The shortest path of t is based on the shortest path of g. 3/ 4 2/ 2 once a node’s shortest path from s is determined, we will never have to revise the decision 1/ 1 a b 2/ 3 2/ 2 1/ 2 d c e e The maximum flow to t is not based on the maximum flows to f and g. f g 1/ 2 2/ 3 t Target 11/12/2018 IT 279

local optimum does not imply global optimum Source Source Source s s s 5/5 2/2 4/5 2/2 5/5 2/2 1/1 0/1 a 1/1 a b a b b 4/4 3/3 1/4 2/3 0/3 3/4 3/3 3/3 1/3 0/3 d 3/3 d c 1/3 d c c Target Target 2/2 5/5 sc max flow = 6 sd max flow = 7 t Target st max flow = 7 11/12/2018 IT 279

A naïve approach: adding all possible paths together Source Source Source s s s 1 2/ 2 1/ 1 2/ 3 2 1 1 1 a a a b b b 1/ 4 4 4 1 2/ 2 1 2/ 3 2 d d d c c c 2/ 3 1/ 1 2/ 2 3 t t t Target Target Target Residual Graph Residual Graph 11/12/2018 IT 279

Not always work. 3/3 2/2 1 1 3 1/4 1 3 2/2 2/ 2 3/3 We are just lucky Source Source s s 3/3 2/2 1 a 1 b a b 3 1/4 1 3 2/2 d c d c 2/ 2 3/3 t t Target Not always work. Target We are just lucky Residual Graph 11/12/2018 IT 279

A wrong choice: 3/ 3 2 2 1 1 3/ 4 1 3 2 3 2 2 3/ 3 2 s s a b a b d c d Source Source s s 3/ 3 2 2 1 a 1 b a b 3/ 4 1 3 2 3 2 d c d c 2 3/ 3 2 t t Target Target 11/12/2018 IT 279

Keep a path for changing decision: Source Source s s 3/ 3 2 2 3 1 a 1 b a b 3/ 4 1 3 2 3 2 3 d c d c 2 3/ 3 2 3 t t Target Target Augmented Graph 11/12/2018 IT 279

Find another path in the augmented graph No more path from s to t Source s s s 2/ 2 3 2 2 3 3 1 a 1 1 b a a b b 2 1 2/ 3 2/ 2 3 1 2 1 2 1 2/ 3 2 2 d 1 1 c d d c c 2/ 2 3 2 2 3 3 t t t Target Augmented Graph new Augmented Graph 11/12/2018 IT 279

+ = Add flows together 3 3 2 2 3 2 2 2 2 2 1 3 3 2 2 Maximum Flow s s Source + = s s 3 s 3 2 2 a b a b a b 3 2 2 2 2 2 1 d c d c d c 3 3 2 2 t t Target t Target Target Maximum Flow 11/12/2018 IT 279

An algorithm for finding the maximum flow of graphs Input: G = (V, E), and s,t  V Let Gmax = (, ), Gaug = G Find a path p from s to t in Gaug If no such p exists, output Gmax and stop the program Add p into Gmax and update Gaug Repeat 1, 2, and, if possible, 3 Complexity: O(c |E|) 11/12/2018 IT 279

IT 328, review graph algorithms A Eulerian cycle (path) is a trail that goes through every edge exactly once. b c a h i j g n k f m e l o d p 11/12/2018 IT 328, review graph algorithms

IT 328, review graph algorithms A Hamiltonian cycle (path) is a cycle that contains every vertex; A graph that contains a Hamiltonian cycle is called Hamiltonian b c a h i j g n k f m e l o d p 11/12/2018 IT 328, review graph algorithms

Back Tracking Going through a Maze Try this java jar 11/12/2018 IT 328