











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
✦ Optimizing routing of packets on the internet: ➭ Vertices = routers, edges = network links with different delays. ➭ What is the routing path with smallest ...
Typology: Exercises
1 / 19
This page cannot be seen from the preview
Don't miss anything!
R. Rao, CSE 326 1
➭ Topological Sort (ver. 1 & 2): Gunning for linear time… ➭ Finding Shortest Paths ➧ Breadth-First Search ➧ Dijkstra’s Method: Greed is good!
✦ Covered in Chapter 9 in the textbook
Some slides based on: CSE 326 by S. Wolfman, 2000
(^143321)
142
322
326 (^370341)
378
401
421
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 3
Any linear ordering in which all the arrows go to the right is a valid solution
R. Rao, CSE 326 7
Example of a cyclic graph: No vertex of in-degree 0
Select
R. Rao, CSE 326 9
Select
R. Rao, CSE 326 13
R. Rao, CSE 326 14
In-Degree array
Adjacency list
R. Rao, CSE 326 15
Break down into total time required to: § Initialize In-Degree array: O(| E |) § Find vertex with in-degree 0: | V | vertices, each takes O(| V |) to search In-Degree array. Total time = O(| V | 2 ) § Reduce In-Degree of all vertices adjacent to a vertex: O(| E |) § Output and mark vertex: O(| V |) Total time = O(| V | 2 + | E |) Quadratic time!
Problem: Need a faster way to find vertices with in-degree 0 instead of searching through entire in-degree array
R. Rao, CSE 326 19
Sort this digraph!
Break down into total time to: Initialize In-Degree array: O(| E |) Initialize Queue with In-Degree 0 vertices: O(| V |) Dequeue and output vertex: | V | vertices, each takes only O(1) to dequeue and output. Total time = O(| V |) Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices: O(| E |) Total time = O(| V | + | E |) Linear running time!
R. Rao, CSE 326 21
✦ Recall definition of a path in a tree – same for graphs ✦ A path is a list of vertices {v 1 , v 2 , …, v (^) n } such that (v (^) i , v (^) i+1 ) is in E for all 0 ≤≤≤≤ i < n.
Seattle
San Francisco Dallas
Chicago
Salt Lake City
Example of a path:
p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}
✦ A simple path repeats no vertices (except the 1 st^ can be the 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: ➭ p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} ✦ A simple cycle is a cycle that repeats no vertices except that the first vertex is also the last ✦ A directed graph with no cycles is called a DAG (directed acyclic graph) E.g. All trees are DAGs ➭ A graph with cycles is often a drag…
R. Rao, CSE 326 25
✦ Plenty of applications ✦ Traveling on a “starving student” budget : What is the cheapest multi-stop airline schedule from Seattle to city X? ✦ Optimizing routing of packets on the internet : ➭ 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 take to minimize total delay due to traffic ✦ Finding the fastest way to get to coffee vendors on campus from your classrooms
Problem: Given a “source” vertex s in an unweighted graph G = ( V , E ), find the shortest path from s to all vertices in G
A
C
B
D
F H
G
E
Find the shortest path from C to: A B C D E F G H
Source
R. Rao, CSE 326 27
✦ Basic Idea: Starting at node s, find vertices that can be reached using 0, 1, 2, 3, …, N-1 edges (works even for cyclic graphs!)
Find the shortest path from C to: A B C D E F G H
On-board example:
A
C
B
D
F H
G
E
✦ Uses a queue to store vertices that need to be expanded
✦ Pseudocode (source vertex is s):
**1. Dist[s] = 0
✦ Running time (same as topological sort) = O(| V | + | E |) (why?)
( Prev allows paths to be reconstructed)
R. Rao, CSE 326 31
✦ Legendary figure in computer science ✦ Some rumors collected from previous classes…
✦ Rumor #1: Supported teaching introductory computer courses without computers (pencil and paper programming)
✦ Rumor #2: Supposedly wouldn’t read his e-mail; so, his staff had to print out his e-mails and put them in his mailbox
E. W. Dijkstra (1930-2002)
Opening sentence of: “ Go To Statement Considered Harmful ” by Edsger W. Dijkstra, Letter to the Editor, Communications of the ACM, Vol. 11, No. 3, March 1968, pp. 147-148.
R. Rao, CSE 326 33
✦ Classic algorithm for solving shortest path in weighted graphs (without negative weights)
✦ Example of a greedy algorithm ➭ Irrevocably makes decisions without considering future consequences ➭ Sound familiar? Not necessarily the best life strategy… but works in some cases (e.g. Huffman encoding)
✦ Basic Idea: ➭ 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
R. Rao, CSE 326 37
A
C
B
D (^) E
2
2
(^11) 9 3 8
3
Initial Final
E No ∞ -
D No ∞ -
C Yes 0 -
B No ∞ -
A No ∞ -
vertex known cost Prev
E Yes 2 C
D Yes 5 E
C Yes 0 -
B Yes 10 A
A Yes 8 D
vertex known cost Prev