Download presentation
Presentation is loading. Please wait.
Published byJanis Blake Modified over 9 years ago
1
Week 12 - Monday
2
What did we talk about last time? Topological sort and cycle detection in directed graphs Connectivity Strong connectivity
6
Student Lecture
8
An airline has to stop running direct flights between some cities But, it still wants to be able to reach all the cities that it can now What’s the set of flights with the lowest total cost that will keep all cities connected? Essentially, what’s the lowest cost tree that keeps the graph connected?
9
This tree is called the minimum spanning tree or MST It has countless applications in graph problems How do we find such a thing?
10
1. Start with two sets, S and V: S has the starting node in it V has everything else 2. Find the node u in V that is closest to any node in S 3. Put the edge to u into the MST 4. Move u from V to S 5. If V is not empty, go back to Step 2
11
A C G E B F D 9 2 2 1 3 5 4 3 3 2 5 8
12
Naïve implementation with an adjacency matrix O(|V| 2 ) Adjacency lists with binary heap O(|E| log |V|) Adjacency lists with Fibonacci heap O(|E| + |V| log |V|)
13
A L I F B E G C J H K D 5 3 11 6 5 1 4 3 8 5 12 9 2 7 1 9 5 4 10 2 4 1
15
How does Google Maps find the shortest route from California to Elizabethtown? Graph theory, of course! It stores a very large graph where locations are nodes and streets (well, parts of streets) are edges
16
We use a weighted graph Weight can represent time, distance, cost: anything, really The shortest path (lowest total weight) is not always obvious 5 3 4 16 2 8 6
17
Take a moment and try to find the shortest path from A to E. The shortest path has cost 14 5 3 4 16 A A B B D D C C E E 2 8 6 A B C D E
18
On a graph of that size, it isn’t hard to find the shortest path A Google Maps graph has millions and millions of nodes How can we come up with an algorithm that will always find the shortest path from one node to another?
19
In 1959, Edsger Dijkstra invented an algorithm to find shortest paths First, a little notation: NotationMeaning sStarting node d(v)d(v) The best distance from s to v found so far d(u, v) The direct distance between nodes u and v S A set which contains the nodes for which we know the shortest path from s V A set which contains the nodes for which we do not yet know the shortest path from s
20
1. Start with two sets, S and V: S is empty V has all the nodes in it 2. Set the distance to all nodes in V to ∞ 3. Set the distance to the starting node s to 0 4. Find the node u in V that is closest to s 5. For every neighbor v of u in V ▪ If d(v) > d(u) + d(u,v) ▪ Set d(v) = d(u) + d(u,v) 6. Move u from V to S 7. If V is not empty, go back to Step 4
21
Noded(u)d(u) A0 B5 C8 D∞ E16 Noded(u)d(u) A0 B5 C7 D11 E16 Noded(u)d(u) A0 B5 C7 D10 E16 Noded(u)d(u) A0 B5 C7 D10 E14 Noded(u)d(u) A0 B5 C7 D10 E14 Noded(u)d(u) A0 B∞ C∞ D∞ E∞ 5 3 4 16 A A B B D D C C E E 2 8 6 Sets SV AB, C, D, E Sets SV A, BC, D, E Sets SV A, B, CD, E Sets SV A, B, C, DE Sets SV A, B, C, D, E Finding the shortest distance from A to all other nodes Sets SV A, B, C, D, E A A
22
Always gets the next closest node, so we know there isn’t a better way to get there Finds the shortest path from a starting node to all other nodes Works even for directed graphs
23
The normal running time for Dijkstra's is O(|V| 2 ) At worst, we may have to update each node in V for each node V that we find the shortest path to A special data structure called a min-priority queue can implement the process of updating priorities faster Total running time of O(|E| + |V| log |V|) Technically faster for sparse graphs Algorithm wizards Fredman and Tarjan created an implementation called a Fibonacci Heap ▪ Actually slow in practice
24
A L I F B E G C J H K D 5 3 11 6 5 1 4 3 8 5 12 9 2 7 1 9 5 4 10 2 4 1
25
The algorithms have very similar forms Here is a graph for which the shortest path tree from A to all other nodes is not an MST A B C D 4 3 3 6 5
27
Matching on bipartite graphs Stable marriage Euler tours
28
Start on Project 4 Form teams by Friday Start on Assignment 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.