Rao Lecture 20 PDF
Rao Lecture 20 PDF
! Items on Todays Lunch Menu: " Topological Sort (ver. 1 & 2): Gunning for linear time " Finding Shortest Paths # Breadth-First Search # Dijkstras Method: Greed is good!
! Covered in Chapter 9 in the textbook
142
Problem: Find an order in which all these courses can be taken. Example: 142 143 378 370 321 341 322 326 421 401
R. Rao, CSE 326
Topological Sort
Topological sorting problem: given digraph G = (V, E) , find a linear ordering of vertices such that: for any edge (v, w) in E, v precedes w in the ordering
B C A F D E A
R. Rao, CSE 326
Any linear ordering in which all the arrows go to the right is a valid solution
D
4
Topological Sort
Topological sorting problem: given digraph G = (V, E) , find a linear ordering of vertices such that: for any edge (v, w) in E, v precedes w in the ordering
B C A F D E A
R. Rao, CSE 326
D
5
10
Select F A B
11
12
Final Result:
A B F C D E
13
C E B C D E Adjacency list E D
D A B C D E F
14
16
17
B 0 A 0 B 1 C F 1 D B C D E Adjacency list E D
Output A B A D
R. Rao, CSE 326
C E
2 E In-Degree array 0 F
18
Store each vertexs In-Degree in an array Initialize a queue with all in-degree zero vertices While there are vertices remaining in the queue: " Dequeue and output a vertex " Reduce In-Degree of all vertices adjacent to it by 1 " Enqueue any of these vertices whose In-Degree became zero B A D E C F Sort this digraph!
19
Paths
! Recall definition of a path in a tree same for graphs ! A path is a list of vertices {v1, v2, , vn} such that
Chicago Seattle Salt Lake City Example of a path: p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}
21
last):
" p = {Seattle, Salt Lake City, San Francisco, Dallas} " p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle}
! A cycle is a path that starts and ends at the same node:
22
Chicago
2
Seattle
2 2
" unweighted vs. weighted " cyclic vs. acyclic " positive weights only vs. negative weights allowed " multiple weight types to optimize " Etc.
! We will look at only a couple of these
" Vertices = routers, edges = network links with different delays " What is the routing path with smallest total delay?
! Hassle-free commuting: Finding what highways and roads to
C Source D
H
26
E B C D E F G H
27
A
1 3 9 1
8 3
Can you find a counterexample (a path) for this graph to show BFS wont work?
29
additional hops Shortest path from C to A: BFS: C A (cost = 9) Minimum Cost Path = C E D A (cost = 8)
2
A
1 3 9 8 1
D
3
30
staff had to print out his e-mails and put them in his mailbox
31
" Irrevocably makes decisions without considering future consequences " Sound familiar? Not necessarily the best life strategy but works in some cases (e.g. Huffman encoding)
33
" Similar to BFS # Each vertex stores a cost for path from source # Vertex to be expanded is the one with least path cost seen so far $ Greedy choice always select current best vertex $ Update costs of all neighbors of selected vertex " But unlike BFS, a vertex already visited may be updated if a better path to it is found
34
Initialize the cost of each node to " Initialize the cost of the source to 0
2
B
1
1 While there are unknown nodes left in the 9 graph 3 1. Select the unknown node N with the C 8 2 lowest cost (greedy choice) D 3 2. Mark N as known E 3. For each node X adjacent to N If (Ns cost + cost of (N, X)) < Xs cost (Prev allows Xs cost = Ns cost + cost of (N, X) paths to be Prev[X] = N //store preceding node reconstructed)
35
Initial
A
3 9 8 1
Final
B
1
C
3
D
R. Rao, CSE 326
E
36
Initial
A
3 9 8 1
Final
B
1
C
3
D
R. Rao, CSE 326
E
37
Questions for Next Time: Does Dijkstras method always work? How fast does it run? Where else in life can I be greedy? To Do: Start Homework Assignment #4 (Dont wait until the last few days!!!) Continue reading and enjoying chapter 9
R. Rao, CSE 326 38