More Graphs Lecture 19 CS2110 – Fall 2009.

Slides:



Advertisements
Similar presentations
Chapter 23 Minimum Spanning Tree
Advertisements

Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
1 Spanning Trees Lecture 20 CS2110 – Spring
Minimum Spanning Tree Algorithms
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Chapter 9: Greedy Algorithms The Design and Analysis of Algorithms.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Chapter 9 Greedy Technique Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Graphs & Graph Algorithms 2 Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CSE 421 Algorithms Richard Anderson Lecture 10 Minimum Spanning Trees.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
1 CSE 417: Algorithms and Computational Complexity Winter 2001 Lecture 11 Instructor: Paul Beame.
Graphs & Graph Algorithms 2 Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
Graphs CS 400/600 – Data Structures. Graphs2 Graphs  Used to represent all kinds of problems Networks and routing State diagrams Flow and capacity.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Lecture 12-2: Introduction to Computer Algorithms beyond Search & Sort.
SPANNING TREES Lecture 21 CS2110 – Spring
Prims’ spanning tree algorithm Given: connected graph (V, E) (sets of vertices and edges) V1= {an arbitrary node of V}; E1= {}; //inv: (V1, E1) is a tree,
Spanning Trees CSIT 402 Data Structures II 1. 2 Two Algorithms Prim: (build tree incrementally) – Pick lower cost edge connected to known (incomplete)
Lecture 19 Greedy Algorithms Minimum Spanning Tree Problem.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
1 Greedy Technique Constructs a solution to an optimization problem piece by piece through a sequence of choices that are: b feasible b locally optimal.
SPANNING TREES Lecture 20 CS2110 – Fall Spanning Trees  Definitions  Minimum spanning trees  3 greedy algorithms (incl. Kruskal’s & Prim’s)
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
SPANNING TREES Lecture 21 CS2110 – Fall Nate Foster is out of town. NO 3-4pm office hours today!
Minimum Spanning Trees CSE 373 Data Structures Lecture 21.
Graphs Upon completion you will be able to:
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
SPANNING TREES Lecture 21 CS2110 – Spring
SPANNING TREES, INTRO. TO THREADS Lecture 23 CS2110 – Fall
Graphs Lecture 19 CS2110 – Spring 2013.
A vertex u is reachable from vertex v iff there is a path from v to u.
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
Minimum Spanning Trees
Spanning Trees Lecture 21 CS2110 – Fall 2016.
Minimum Spanning Trees and Shortest Paths
Cinda Heeren / Geoffrey Tien
Introduction to Graphs
Spanning Trees, greedy algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
Short paths and spanning trees
Minimum-Cost Spanning Tree
Graphs Lecture 18 CS2110 – Fall 2009.
Minimum Spanning Tree.
Graphs & Graph Algorithms 2
Graphs Chapter 13.
Algorithms (2IL15) – Lecture 5 SINGLE-SOURCE SHORTEST PATHS
Minimum-Cost Spanning Tree
Chapter 11 Graphs.
Autumn 2015 Lecture 10 Minimum Spanning Trees
Minimum Spanning Tree Algorithms
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Autumn 2016 Lecture 10 Minimum Spanning Trees
The Greedy Approach Young CS 530 Adv. Algo. Greedy.
Spanning Trees Lecture 20 CS2110 – Spring 2015.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Winter 2019 Lecture 10 Minimum Spanning Trees
Algorithm Course Dr. Aref Rashad
CSE 332: Minimum Spanning Trees
Spanning Trees, greedy algorithms
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Prims’ spanning tree algorithm
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

More Graphs Lecture 19 CS2110 – Fall 2009

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

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)

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?

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)); }

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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; }