Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Graphs Lecture 19 CS2110 – Fall 2009.

Similar presentations


Presentation on theme: "More Graphs Lecture 19 CS2110 – Fall 2009."— Presentation transcript:

1 More Graphs Lecture 19 CS2110 – Fall 2009

2 Representations of Graphs
1 2 4 3 Adjacency List Adjacency Matrix 1 2 3 4 2 3 4 1

3 Adjacency Matrix or Adjacency List?
n = number of vertices m = number of edges d(u) = outdegree of u Adjacency Matrix Uses space O(n2) Can iterate over all edges in time O(n2) Can answer “Is there an edge from u to v?” in O(1) time Better for dense graphs (lots of edges) Adjacency List Uses space O(m+n) Can iterate over all edges in time O(m+n) Can answer “Is there an edge from u to v?” in O(d(u)) time Better for sparse graphs (fewer edges)

4 Shortest Paths in Graphs
Finding the shortest (min-cost) path in a graph is a problem that occurs often Find the shortest route between Ithaca and West Lafayette, IN Result depends on our notion of cost Least mileage Least time Cheapest Least boring All of these “costs” can be represented as edge weights How do we find a shortest path?

5 Dijkstra’s Algorithm dijkstra(s) { // Note: c(s,t) = cost of the s,t edge if present // Integer.MAX_VALUE otherwise D[s] = 0; D[t] = c(s,t), t ≠ s; mark s; while (some vertices are unmarked) { v = unmarked node with smallest D; mark v; for (each w adjacent to v) { D[w] = min(D[w], D[v] + c(v,w)); }

6 Dijkstra’s Algorithm 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.4 1.5

7 Dijkstra’s Algorithm X 2.4 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5

8 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 4.6

9 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 4.6

10 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 4.6

11 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 2.5

12 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 2.5

13 Dijkstra’s Algorithm X 1.6 2.4 1 2 1.5 0.9 0.1 4 3 3.1 1.5 2.5

14 Proof of Correctness The following are invariants of the loop:
X is the set of marked nodes For u ∈ X, D(u) = d(s,u) For u ∈ X and v ∉ X, d(s,u) ≤ d(s,v) For all u, D(u) is the length of the shortest path from s to u such that all nodes on the path (except possibly u) are in X Implementation: Use a priority queue for the nodes not yet taken – priority is D(u)

15 Shortest Paths for Unweighted Graphs – A Special Case
Use breadth-first search Time is O(n + m) in adj list representation, O(n2) in adj matrix representation S B A C D E F

16 Undirected Trees An undirected graph is a tree if there is exactly one simple path between any pair of vertices

17 Facts About Trees |E| = |V| – 1 connected no cycles
In fact, any two of these properties imply the third, and imply that the graph is a tree

18 Spanning Trees A spanning tree of a connected undirected graph (V,E) is a subgraph (V,E') that is a tree

19 Spanning Trees A spanning tree of a connected undirected graph (V,E) is a subgraph (V,E') that is a tree Same set of vertices V E' ⊆ E (V,E') is a tree

20 Finding a Spanning Tree
A subtractive method Start with the whole graph – it is connected If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?) Repeat until no more cycles

21 Finding a Spanning Tree
A subtractive method Start with the whole graph – it is connected If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?) Repeat until no more cycles

22 Finding a Spanning Tree
A subtractive method Start with the whole graph – it is connected If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?) Repeat until no more cycles

23 Finding a Spanning Tree
An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

24 Finding a Spanning Tree
An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

25 Finding a Spanning Tree
An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

26 Finding a Spanning Tree
An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

27 Finding a Spanning Tree
An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

28 Finding a Spanning Tree
An additive method Start with no edges – there are no cycles If more than one connected component, insert an edge between them – still no cycles (why?) Repeat until only one component

29 Minimum Spanning Trees
Suppose edges are weighted, and we want a spanning tree of minimum cost (sum of edge weights) Some graphs have exactly one minimum spanning tree. Others have multiple trees with the same cost, any of which is a minimum spanning tree

30 Minimum Spanning Trees
Suppose edges are weighted, and we want a spanning tree of minimum cost (sum of edge weights) 1 14 2 3 72 Useful in network routing & other applications For example, to stream a video 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

31 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

32 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

33 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

34 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 100 7 33 34 9 28 64 22 24 54 4 15 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

35 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 72 7 33 34 9 28 64 22 24 54 4 15 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

36 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 7 9 22 24 54 4 15 21 11 6 13 12 8 16 10 25 5

37 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 3 7 9 54 4 15 11 6 13 12 8 16 10 25 5

38 3 Greedy Algorithms A. Find a max weight edge – if it is on a cycle, throw it out, otherwise keep it 1 14 2 7 9 54 4 11 6 12 8 16 10 25 5

39 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

40 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

41 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

42 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

43 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

44 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

45 3 Greedy Algorithms B. Find a min weight edge – if it forms a cycle with edges already taken, throw it out, otherwise keep it 1 14 2 3 72 Kruskal's algorithm 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

46 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

47 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

48 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

49 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

50 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

51 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

52 3 Greedy Algorithms C. Start with any vertex, add min weight edge extending that connected component that does not form a cycle 1 14 2 3 72 Prim's algorithm (reminiscent of Dijkstra's algorithm) 100 7 33 34 9 28 64 22 24 54 4 15 101 21 11 49 51 6 13 62 32 12 8 16 27 10 25 5 40 66

53 3 Greedy Algorithms When edge weights are all distinct, or if there is exactly one minimum spanning tree, the 3 algorithms all find the identical tree 1 14 2 7 9 54 4 11 6 12 8 16 10 25 5

54 Prim’s Algorithm O(n2) for adj matrix O(m + n log n) for adj list
D[s] = 0; mark s; //start vertex while (some vertices are unmarked) { v = unmarked vertex with smallest D; mark v; for (each w adj to v) { D[w] = min(D[w], c(v,w)); } O(n2) for adj matrix While-loop is executed n times For-loop takes O(n) time O(m + n log n) for adj list Use a PQ Regular PQ produces time O(n + m log m) Can improve to O(m + n log n) using a fancier heap

55 Greedy Algorithms These are examples of Greedy Algorithms
The Greedy Strategy is an algorithm design technique Like Divide & Conquer Greedy algorithms are used to solve optimization problems The goal is to find the best solution Works when the problem has the greedy-choice property A global optimum can be reached by making locally optimum choices Example: the Change Making Problem: Given an amount of money, find the smallest number of coins to make that amount Solution: Use a Greedy Algorithm Give as many large coins as you can This greedy strategy produces the optimum number of coins for the US coin system Different money system ⇒ greedy strategy may fail Example: old UK system

56 Similar Code Structures
Breadth-first-search (bfs) best: next in queue update: D[w] = D[v]+1 Dijkstra’s algorithm best: next in PQ update: D[w] = min(D[w], D[v]+c(v,w)) Prim’s algorithm update: D[w] = min(D[w], c(v,w)) while (some vertices are unmarked) { v = best of unmarked vertices; mark v; for (each w adj to v) update w; }


Download ppt "More Graphs Lecture 19 CS2110 – Fall 2009."

Similar presentations


Ads by Google