{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L19cs2110fa09-6up

L19cs2110fa09-6up - 1 Representations of Graphs 2 1 2 4 3...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
03/11/2009 1 1 MORE GRAPHS Lecture 19 CS2110 – Fall 2009 Representations of Graphs Adjacenc List Adjacenc Matri 1 2 3 4 2 2 3 2 4 3 1 2 3 4 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0 1 2 3 4 1 2 3 4 Adjacency List Adjacency Matrix Adjacency Matrix or Adjacency List? n = number of vertices m = number of edges d(u) = outdegree of u 3 Adjacency Matrix Uses space O(n 2 ) Can iterate over all edges in time O(n 2 ) 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 4 east eage 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; 5 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)); } } } 2.4 1 2 2.4 0 9 X Dijkstra’s Algorithm 6 1.5 3 4 0.9 1.5 3.1 0.1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
03/11/2009 2 2.4 1 2 2.4 0 9 X Dijkstra’s Algorithm 7 1.5 3 4 0.9 1.5 3.1 0.1 1.6 1 2 2.4 0 9 X Dijkstra’s Algorithm 8 1.5 3 4 0.9 1.5 3.1 0.1 4.6 1.6 1 2 2.4 0 9 X Dijkstra’s Algorithm 9 1.5 3 4 0.9 1.5 3.1 0.1 4.6 1.6 1 2 2.4 0 9 X Dijkstra’s Algorithm 10 1.5 3 4 0.9 1.5 3.1 0.1 4.6 1.6 1 2 2.4 0 9 X Dijkstra’s Algorithm 11 1.5 3 4 0.9 1.5 3.1 0.1 2.5 1.6 1 2 2.4 0 9 X Dijkstra’s Algorithm 12 1.5 3 4 0.9 1.5 3.1 0.1 2.5
Background image of page 2
03/11/2009 3 1.6 1 2 2.4 0 9 X Dijkstra’s Algorithm 13 1.5 3 4 0.9 1.5 3.1 0.1 2.5 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) Proof of Correctness 14 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(n 2 ) 15 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 16 Facts About Trees • |E| = |V| – 1 • connected • no cycles 17 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 18
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon