Beruflich Dokumente
Kultur Dokumente
Todays Topics: Dijkstras Shortest Path Algorithm Depth First Search Spanning Trees Minimum Spanning Trees Prims Algorithm
Given a graph G = (V, E) and a source vertex s in V, find the minimum cost paths from s to every vertex in V
2
A
9 3 8
B
1 1
C
3
Source
2
Initialize the cost of each node to Initialize the cost of the source to 0
2
While there are unknown nodes left in the A 1 1 graph 9 1. Select the unknown node N with the 3 C lowest cost (greedy choice) 8 2 2. Mark N as known D 3 3. For each node A adjacent to N E If (Ns cost + cost of (N, A)) < As cost As cost = Ns cost + cost of (N, A) (Prev allows Prev[A] = N //store preceding node paths to be reconstructed) R. Rao, CSE 373 3
Initial
A
3 9 8 1
Final
B
1
C
3
D
R. Rao, CSE 373
E
4
Main loop: While there are unknown nodes left in the graph |V| times 1. Select the unknown node N with the lowest cost O(|V|) 2. Mark N as known O(|E|) total 3. For each node A adjacent to N If (Ns cost + cost of (N, A)) < As cost As cost = Ns cost + cost of (N, A)
Total time = |V| (O(|V|)) +O(|E|) = O(|V|2 + |E|) Dense graph: |E| = (|V|2) ! Total time = O(|V|2) = O(|E|) Sparse graph: |E| = (|V|) ! Total time = O(|V|2) = O(|E|2) Quadratic! Can we do better?
R. Rao, CSE 373 5
Dijkstras algorithm is an example of a greedy algorithm Greedy algorithms always make choices that currently seem the best
Short-sighted no consideration of long-term or global issues Locally optimal does not always mean globally optimal
In Dijkstras case choose the least cost node, but what if there is another path through other vertices that is cheaper? Can prove: Never happens if all edge weights are positive
If the path to G is the next shortest path, the path to P must be at least as long. Therefore, any path through P to G cannot be shorter!
R. Rao, CSE 373 9
10
Gotcha!!
11
A
3 9 -5
B
1
A
E
2 -5
B
1
10
C
-10
Dijkstra: C!D (cost = -5) Least cost path: C!E!D (cost = -8)
R. Rao, CSE 373
Negative cycles: Whats the shortest path from A to E? (or to B, C, or D, for that matter)
12
We used Breadth First Search for finding shortest paths in an unweighted graph
Use a queue to explore neighbors of source vertex, neighbors of each neighbor, and so on: 1 edge away, two edges away, etc.
13
DFS Pseudocode
C D
DFS(C)
E
C DFS(C)
E
14
143
142
378
421 401
15
143
142
16
DFS(C) C D E C
E
17
Minimum spanning tree: the spanning tree with the least total edge cost
18
1 10 2 1 6 2 1 6 5
19
5 1 8 3 3 4
Lots of applications Minimize length of gas pipelines between cities Find cheapest way to wire a house (with minimum cable) Find a way to connect various routers on a network that minimizes total delay Etc
20
v0
10 8 1 6
1 5 1 3 3 4
4 5
21
v0
10 2 1 6
1 5
v
8 3
1 3 4
4 5
22
v0
10 2 1 6 2 1 6
1 5
v
8 3
1 3 4
4 5
23
v0
10 2 1 6 2 1 6 8
1 5 1 3 3 4
4 5
24
v0
10 2 1 6 2 1 6 8
1 5 1 3 3 4
4 5
25
v0
10 2 1 6 2 1 6 8
1 5 1 3 3 4
4 5
26
v0
10 2 1 6 2 1 6 8
1 5 1 3 3 4
4 5
27
1 2 1 4 5
28
Next Class: Analysis of Prims Algorithm Kruskal takes a bow faster MST To Do: Programming Assignment #2 (Dont wait until the last few days!!!) Continue chapter 9
R. Rao, CSE 373 29