Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001.

Similar presentations


Presentation on theme: "CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001."— Presentation transcript:

1 CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001

2 GraphsCSE 326 Autumn 20012 Graph … ADT? Graphs are a formalism useful for representing relationships between things  A graph G is represented as G = (V, E)  V is a set of vertices: {v 1, …, v n }  E is a set of edges: {e 1, …, e m } where each e i connects two vertices (v i1, v i2 )  Operations include:  iterating over vertices  iterating over edges  iterating over vertices adjacent to a specific vertex  asking whether an edge exists connects two vertices Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)}

3 GraphsCSE 326 Autumn 20013 Graph Applications Storing things that are graphs by nature  distance between cities  airline flights, travel options  relationships between people, things  distances between rooms in Clue Compilers  callgraph - which functions call which others  dependence graphs - which variables are defined and used at which statements Almost anything involving discrete entities!

4 GraphsCSE 326 Autumn 20014 Total Order 1 2 3 4 5 6 7 AB means A must go before B

5 GraphsCSE 326 Autumn 20015 Partial Order: Planning a Trip check in airport call taxi taxi to airport reserve flight pack bags take flight locate gate ?

6 GraphsCSE 326 Autumn 20016 Topological Sort Given a graph, G = (V, E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it. check in airport call taxi taxi to airport reserve flight pack bags take flight locate gate Beware the Catch-22!

7 GraphsCSE 326 Autumn 20017 Topo-Sort Take One Label each vertex’s in-degree (# of inbound edges) While there are vertices remaining  Pick a vertex with in-degree of zero and output it  Reduce the in-degree of all vertices adjacent to it  Remove it from the list of vertices Runtime?

8 GraphsCSE 326 Autumn 20018 Topo-Sort Take Two Label each vertex’s in-degree Put all in-degree-zero vertices in a queue While there are vertices remaining in the queue Pick a vertex v with in-degree of zero and output it Reduce the in-degree of all vertices adjacent to v Put any of these with new in-degree zero on the queue Remove v from the queue Runtime?

9 GraphsCSE 326 Autumn 20019 Graph Representations  List of vertices + list of edges  2-D matrix of vertices (marking edges in the cells) “adjacency matrix”  List of vertices each with a list of adjacent vertices “adjacency list”

10 GraphsCSE 326 Autumn 200110 Adjacency Matrix A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to v Han Leia Luke HanLukeLeia Han Luke Leia runtime: space requirements:

11 GraphsCSE 326 Autumn 200111 Adjacency List A |V| -ary list (array) in which each entry stores a list (linked list) of all adjacent vertices (or edges) Han Leia Luke Han Luke Leia runtime: space requirements:

12 GraphsCSE 326 Autumn 200112  In directed graphs, edges have a specific direction: (aka “di-graphs”)  In undirected graphs, they don’t (edges are two-way):  Vertices u and v are adjacent if (u, v)  E Directed vs. Undirected Graphs Luke Han Leia Han Leia Luke

13 GraphsCSE 326 Autumn 200113 Weighted Graphs 20 30 35 60 Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton In a weighted graph, each edge has an associated weight or cost.

14 GraphsCSE 326 Autumn 200114 Paths A path is a list of vertices {v 1, v 2, …, v n } such that (v i, v i+1 )  E for all 0  i < n. Seattle San Francisco Dallas Chicago Salt Lake City p = {SEA, SLC, CHI, DAL, SFO, SEA}

15 GraphsCSE 326 Autumn 200115 Path Length and Cost Path length: the number of edges in the path Path cost: the sum of the costs of each edge Seattle San Francisco Dallas Chicago Salt Lake City 3.5 2 2 2.5 3 2 length(p) = 5 cost(p) = 11.5 p = {SEA, SLC, CHI, DAL, SFO, SEA}

16 GraphsCSE 326 Autumn 200116 Simple Paths and Cycles A simple path repeats no vertices (except that the first can be the last):  p = {Seattle, Salt Lake City, San Francisco, Dallas}  p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A cycle is a path that starts and ends at the same node:  p = {Seattle, Salt Lake City, Dallas, San Francisco, Seattle} A simple cycle is a cycle that repeats no vertices except that the first vertex is also the last (in undirected graphs, no edge can be repeated)

17 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 Di-graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices Connectivity

18 GraphsCSE 326 Autumn 200118 Graph Density A sparse graph has O(|V|) edges A dense graph has  (|V| 2 ) edges Anything in between is either sparsish or densy depending on the context.

19 GraphsCSE 326 Autumn 200119 Trees as Graphs Every tree is a graph with some restrictions:  the tree is directed  there are no cycles (directed or undirected)  there is a directed path from the root to every node A B DE C F HG JI

20 GraphsCSE 326 Autumn 200120 Directed Acyclic Graphs (DAGs)  DAGs are directed graphs with no cycles  Trees  DAGs  Graphs main() add() access() mult() read()

21 GraphsCSE 326 Autumn 200121 Single Source, Shortest Path Given a graph G = (V, E) and a vertex s  V, find the shortest path from s to every vertex in V  (Strangely, this is also typically the best way to find the shortest path from s to any vertex in V ) Many variations:  weighted vs. unweighted  cyclic vs. acyclic  positive weights only vs. negative weights allowed  multiple weight types to optimize Many applications!

22 GraphsCSE 326 Autumn 200122 The Trouble with Negative-Weight Cycles AB CD E 2 10 1 -5 2 What’s the shortest path from A to E? (or to B, C, or D, for that matter)

23 GraphsCSE 326 Autumn 200123 Unweighted Shortest Path Assume source vertex is C … A C B D FH G E Distance to: A B C D E F G H

24 GraphsCSE 326 Autumn 200124 Dijkstra, Edsger Wybe Legendary figure in computer science; now a professor at University of Texas. Supports teaching introductory computer courses without computers (pencil and paper programming) Supposedly wouldn’t (until recently) read his e-mail; so, his staff had to print out messages and put them in his box.

25 GraphsCSE 326 Autumn 200125 Dijkstra’s Algorithm for Single Source, Shortest Path Classic algorithm for solving shortest path in weighted graphs without negative weights A greedy algorithm (irrevocably makes decisions without considering future consequences) Intuition:  shortest path from source vertex to itself is 0  cost of going to adjacent nodes is at most edge weights  cheapest of these must be shortest path to that node  update paths for new node and continue picking cheapest path

26 GraphsCSE 326 Autumn 200126 Intuition in Action A C B D FH G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7

27 GraphsCSE 326 Autumn 200127 Dijkstra Pseudocode Initialize the cost of each node to  Initialize the cost of the source to 0 While there are unknown nodes left in the graph Select the unknown node with the lowest cost: n Mark n as known For each node a which is adjacent to n a’s cost = min(a’s old cost, n’s cost + cost of (n, a))

28 GraphsCSE 326 Autumn 200128 Dijkstra’s Algorithm in Action A C B D FH G E 2 2 3 2 1 1 4 10 8 1 1 9 4 2 7

29 GraphsCSE 326 Autumn 200129 The Known Cloud G Next shortest path from inside the known cloud P Better path to G? Not! The Cloud Proof  But, if path to G is shortest, path to P must be at least as long.  So, how can the path through P to G be shorter? Source

30 GraphsCSE 326 Autumn 200130 Inside the Cloud (Proof) Prove by induction on # of nodes in the cloud:  Initial cloud is just the source with shortest path 0  Assume: Everything inside the cloud has the correct shortest path  Inductive step: once we prove the shortest path to G is correct, we add it to the cloud Negative weights blow this proof away! Why?

31 GraphsCSE 326 Autumn 200131 Data Structures for Dijkstra’s Algorithm Select the unknown node with the lowest cost findMin/deleteMin a’s cost = min(a’s old cost, …) decreaseKey find by name |V| times: |E| times: Data structure(s)? Runtime?

32 GraphsCSE 326 Autumn 200132 Dijkstra Pseudocode Redux Initialize the cost of each node to  s.cost = 0 pqueue.insert(s) While (!pqueue.empty) n = pqueue.deleteMin() For (each node a which is adjacent to n) if (n.cost + edge[n,a].cost < a.cost) then a.cost = n.cost + edge[n,a].cost a.prev = n if (a is in the pqueue) then pqueue.decreaseKey(a) else pqueue.insert(a)

33 GraphsCSE 326 Autumn 200133 Dijkstra’s Algorithm: Priority Queue Options Recall  Dijkstra’s uses |V| deleteMins and |E| decreaseKeys Binary heap  Straightforward implementation Fibonacci heap  Somewhat like Binomial Queues with lazy merges  Amortized O(1) decreaseKey, O(log n) deleteMin  Runtime with Fibonacci heaps?  However, complex implementation, high-constant-overhead Unsorted linked list???  For dense graphs, might be the way to go! Why?


Download ppt "CSE 326 Graphs David Kaplan Dept of Computer Science & Engineering Autumn 2001."

Similar presentations


Ads by Google