2/11/2014 Announcements (2/12/14) • Exams • Return at end of class CSE 332: Graphs • Mean 62.5, Median 63, sd 7.2 • HW 5 available • Project 2B due Thursday night Richard Anderson, Steve Seitz • Reading for this lecture: Chapter 9. Winter 2014 1 2 Graphs Graphs • A formalism for representing relationships between D objects Notation D A |V| = number of vertices C Graph G = (V,E) A |E| = number of edges C – Set of vertices : B V = {v 1 ,v 2 ,…, v n } B • v is adjacent to u if (u,v) ∈ E – Set of edges : V = {A, B, C, D} V = {A, B, C, D} – neighbor of = adjacent to E = {e 1 ,e 2 ,…, e m } E = {(C, B), E = {(C, B), where each e i connects one – Order matters for directed edges (A, B), (A, B), vertex to another (v j ,v k ) • It is possible to have an edge (v,v) , (B, A) (B, A) (C, D)} (C, D)} called a loop . For directed edges , (v j ,v k ) and (v k ,v j ) are distinct. – We will assume graphs without loops. (More on this later…) 3 4 Examples of Graphs Directed Graphs In directed graphs (a.k.a., digraphs ), edges have a direction: For each, what are the vertices and edges? D D • The web A A or C C • Facebook 2 edges B B here • Highway map Thus, (u,v) ∈ E does not imply (v,u) ∈ E . • Airline routes I.e., v adjacent to u does not imply u adjacent to v . • Call graph of a program In-degree of a vertex: number of inbound edges. Out-degree of a vertex : number of outbound edges. • … 5 6 1
2/11/2014 Undirected Graphs Weighted Graphs In undirected graphs, edges have no specific direction (edges are always two-way): Each edge has an associated weight or cost. D 20 Clinton A C Mukilteo B 30 Kingston Edmonds Thus, (u,v) ∈ E does imply (v,u) ∈ E . Only one of these edges needs to be in the set; the other is implicit. 35 Bainbridge Seattle Degree of a vertex: number of edges containing that vertex. 60 (Same as number of adjacent vertices.) Bremerton 7 8 Paths and Cycles Path Length and Cost • A path is a list of vertices {w 1 , w 2 , …, w q } such that (w i , w i+1 ) ∈ E for all 1 ≤ i < q • Path length : the number of edges in the path • A cycle is a path that begins and ends at the same node • Path cost : the sum of the costs of each edge Chicago Chicago 3.5 Seattle Seattle 2 2 For path P: Salt Lake City 2 Salt Lake City length(P) = 5 2.5 2.5 2.5 cost(P) = 11.5 3 San Francisco San Francisco Dallas Dallas P = {Seattle, Salt Lake City, Chicago, How would you ensure that length(p)=cost(p) for all p? 9 10 Dallas, San Francisco, Seattle} Simple Paths and Cycles Paths/Cycles in Directed Graphs A simple path repeats no vertices (except that the first can Consider this directed graph: also be the last): P = {Seattle, Salt Lake City, San Francisco, Dallas} D P = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A C A cycle is a path that starts and ends at the same node: P = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} B P = {Seattle, Salt Lake City, Seattle, San Francisco, Seattle} Is there a path from A to D? A simple cycle is a cycle that is also a simple path (in undirected graphs, no edge can be repeated). Does the graph contain any cycles? 11 12 2
2/11/2014 Undirected Graph Connectivity Directed Graph Connectivity Undirected graphs are connected if there is a path between any two vertices: Directed graphs are strongly connected if there is a path from any one vertex to any other. Directed graphs are weakly connected Connected graph Disconnected graph if there is a path between any two vertices, ignoring direction. A complete undirected graph has an edge between every pair of vertices: A complete directed graph has a directed edge between every pair of vertices. (Again, complete = fully connected .) (Complete = fully connected ) 13 14 Rooted Trees Trees as Graphs We are more accustomed to: •Rooted trees (a tree node that is “special”) • Directed edges from parents to children (parent closer to root). D E A tree is a graph that is: A rooted tree (root indicated in red) Rooted tree with directed B – undirected drawn two ways edges from parents to children. – acyclic D E A – connected A B A B C B C C A D E F D E F C F Hey, that doesn’t look like a tree! G H G H F Characteristics of this one? G H 15 16 G H |E| and |V| Directed Acyclic Graphs (DAGs) How many edges |E| in a graph with |V| vertices? DAGs are directed What if the graph is directed? main() graphs with no (directed) cycles. What if it is undirected and connected? mult() Can the following bounds be simplified? add() – Arbitrary graph: O(|E| + |V|) Aside: If program call- – Arbitrary graph: O(|E| + |V| 2 ) graph is a DAG, then all – Undirected, connected: O(|E| log|V| + |V| log|V|) procedure calls can be in- access() read() lined Some (semi-standard) terminology: – A graph is sparse if it has O(|V|) edges (upper bound). – A graph is dense if it has (|V| 2 ) edges. 17 18 3
2/11/2014 What’s the data structure? Representation 2: Adjacency List A list (array) of length |V| in which each entry stores a list • Common query: which edges are adjacent to a vertex (linked list) of all adjacent vertices D A A C B B C D Runtimes: Iterate over vertices? Iterate over edges? Space requirements? Best for what kinds of graphs? Iterate edges adj. to vertex? 19 20 Existence of edge? Representing Undirected Graphs Representation 1: Adjacency Matrix A |V| x |V| matrix M in which an element M[u,v] is true What do these reps look like for an undirected graph? if and only if there is an edge from u to v D A B C D A C D A Adjacency matrix: B A Adjacency list: C B A B C D A A B C B Runtimes: B D Iterate over vertices? C C Iterate over edges? Space requirements? Iterate edges adj. to vertex? Best for what kinds of graphs? D D 21 22 Existence of edge? Some Applications: Some Applications: Moving Around Washington Moving Around Washington What’s the shortest route to from Seattle to Pullman? What’s the quickest way to get from Seattle to Pullman? Edge labels: Edge labels: 23 24 4
2/11/2014 Some Applications: Some Applications: Reliability of Communication Bus Routes in Downtown Seattle If we’re at 3 rd and Pine, how can we get to If Wenatchee’s phone exchange goes down , 1 st and University using Metro? can Seattle still talk to Pullman? How about 4 th and Seneca? 25 26 Application: Topological Sort Topological Sort: Take One Given a graph, G = (V,E) , output all the vertices in V sorted so that no vertex is output before any other vertex with an edge to it. 1. Label each vertex with its in-degree (# inbound edges) 2. While there are vertices remaining: CSE 403 a. Choose a vertex v of in-degree zero ; output v CSE 321 CSE 322 b. Reduce the in-degree of all vertices adjacent to v CSE 421 c. Remove v from the list of vertices CSE 326 CSE 142 CSE 143 CSE 341 CSE 451 CSE 378 CSE 457 CSE 303 CSE 370 CSE 467 Runtime: What kind of input graph is allowed? Is the output unique? 27 28 void Graph::topsort(){ 142 Vertex v, w; 143 321 labelEachVertexWithItsInDegree(); CSE 403 CSE 321 CSE 322 341 CSE 421 378 for (int counter=0; counter < NUM_VERTICES; CSE 326 CSE 142 CSE 143 CSE 341 counter++){ 370 CSE 451 v = findNewVertexOfDegreeZero(); 322 CSE 378 CSE 457 CSE 303 326 v.topologicalNum = counter; CSE 370 CSE 467 for each w adjacent to v 303 w.indegree--; 403 } 421 } 451 457 467 29 30 5
2/11/2014 Topological Sort: Take Two void Graph::topsort(){ Queue q(NUM_VERTICES); int counter = 0; Vertex v, w; 1. Label each vertex with its in-degree labelEachVertexWithItsIn-degree(); 2. Initialize a queue Q to contain all in-degree zero q.makeEmpty(); intialize the vertices for each vertex v queue 3. While Q not empty if (v.indegree == 0) a. v = Q .dequeue; output v q.enqueue(v); b. Reduce the in-degree of all vertices adjacent to v get a vertex with c. If new in-degree of any such vertex u is zero while (!q.isEmpty()){ indegree 0 Q .enqueue( u ) v = q.dequeue(); v.topologicalNum = ++counter; for each w adjacent to v insert new if (--w.indegree == 0) Note: could use a stack, list, set, eligible q.enqueue(w); box, … instead of a queue vertices } Runtime: } 31 32 6
Recommend
More recommend