{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L19cs2110fa08 - More Graphs Lecture 19 CS2110 Fall 2008...

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

View Full Document Right Arrow Icon
More Graphs Lecture 19 CS2110 – Fall 2008
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
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 Representations of Graphs Adjacency List Adjacency Matrix 1 2 3 4
Background image of page 2
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)
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
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?
Background image of page 4
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)); } }
Background image of page 5

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

View Full Document Right Arrow Icon
1 2 3 4 2.4 0.9 1.5 3.1 0.1 X
Background image of page 6
2.4 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X
Background image of page 7

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

View Full Document Right Arrow Icon
2.4 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X
Background image of page 8
1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 4.6
Background image of page 9

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

View Full Document Right Arrow Icon
1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 4.6
Background image of page 10
1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 4.6
Background image of page 11

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

View Full Document Right Arrow Icon
1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.5
Background image of page 12
1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.5
Background image of page 13

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

View Full Document Right Arrow Icon
1.6 1.5 1 2 3 4 2.4 0.9 1.5 3.1 0.1 X 2.5
Background image of page 14
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
Background image of page 15

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

View Full Document Right Arrow Icon
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
Background image of page 16
Undirected Trees • An undirected graph is a tree if there is exactly one simple path between any pair of vertices
Background image of page 17

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

View Full Document Right Arrow Icon
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
Background image of page 18
Spanning Trees A spanning tree of a connected undirected graph (V,E) is a subgraph (V,E') that is a tree
Background image of page 19

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

View Full Document Right Arrow Icon
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
Background image of page 20
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}