Download presentation
Presentation is loading. Please wait.
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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 sc max flow = 6 sd max flow = 7 t Target st max flow = 7 11/12/2018 IT 279
13
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
14
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
15
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
16
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
17
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
18
+ = 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
19
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
20
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
21
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
22
Back Tracking Going through a Maze Try this java jar 11/12/2018 IT 328
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.