L19cs2110fa08-6up

L19cs2110fa08-6up - More Graphs Lecture 19 CS2110 – Fall...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: More Graphs Lecture 19 CS2110 – Fall 2008 2 3 2 4 3 1 2 3 4 0 1 0 1 0 0 1 0 0 0 0 1 1 1 2 3 4 1 2 3 4 Representations of Graphs Adjacency List Adjacency Matrix 1 2 3 4 Adjacency Matrix or Adjacency List? n = number of vertices m = number of edges d(u) = outdegree of u • 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 • 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) { 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)); } } 2.4 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X " 2.4 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X " 1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 4.6 1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 4.6 1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 4.6 1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.5 1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.5 1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.5 The following are invariants of the loop: • 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) Proof of Correctness Shortest Paths for Unweighted Graphs – A Special Case • Use breadth-first search • Time is O(n + m) in adj list representation, O(n 2 ) 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 • If there is a cycle, pick an edge on the cycle, throw it out – the graph is still connected (why?)connected (why?...
View Full Document

This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell.

Page1 / 10

L19cs2110fa08-6up - More Graphs Lecture 19 CS2110 – Fall...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online