Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS

Similar presentations


Presentation on theme: "CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS"— Presentation transcript:

1 CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS
Henry Kautz Winter Quarter 2002

2 Midterm Mean: 77 Std. Dev: 11 High score: 94

3 Outline Graphs (TO DO: READ WEISS CH 9) Graph Data Structures
Graph Properties Topological Sort Graph Traversals Depth First Search Breadth First Search Iterative Deepening Depth First Shortest Path Problem Dijkstra’s Algorithm Alright, we’ll start with a little treat that has been a long time in the coming. Then, I’ll define what a graph is. We’ll get a taste of an algorithm before we actually hit all the terminology and data structures. Finally, I’ll start out the shortest path problem. I’ll finish that on Friday.

4 Graph ADT Graphs are a formalism for representing relationships between objects a graph G is represented as G = (V, E) V is a set of vertices E is a set of edges operations include: iterating over vertices iterating over edges iterating over vertices adjacent to a specific vertex asking whether an edge exists connected two vertices Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)} I’m not convinced this is really an ADT, but it is certainly an important structure.

5 What Graph is THIS? Graphs are used all over the place. I think Zasha will fill you in a bit more on that.

6 ReferralWeb (co-authorship in scientific papers)

7 Biological Function Semantic Network

8 Graph Representation 1: 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 Luke Leia Han Han Leia Luke Luke iterate over vertices iterate over edges iterate over vertices adj. to a vertex check whether an edge exists Runtime: iterate over vertices iterate ever edges iterate edges adj. to vertex edge exists? Leia Space requirements:

9 Graph Representation 2: Adjacency List
A |V|-ary list (array) in which each entry stores a list (linked list) of all adjacent vertices Han Han Leia Luke Luke Runtime: iterate over vertices iterate ever edges iterate edges adj. to vertex edge exists? Leia space requirements:

10 Directed vs. Undirected Graphs
In directed graphs, edges have a specific direction: In undirected graphs, they don’t (edges are two-way): Vertices u and v are adjacent if (u, v)  E Han Luke Leia Han Luke Leia

11 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. Graph density is a measure of the number of edges. Remember that we need to analyze graphs in terms of both |E| and |V|. If we know the density of the graph, we can dispense with that.

12 Weighted Graphs Each edge has an associated weight or cost. Clinton
20 Mukilteo Kingston 30 Edmonds How could we store weights in a matrix? List? Bainbridge 35 Seattle 60 Bremerton There may be more information in the graph as well.

13 Paths and Cycles A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1)  E for all 0  i < n. A cycle is a path that begins and ends at the same node. Chicago Seattle Salt Lake City San Francisco Dallas p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle}

14 Path Length and Cost Path length: the number of edges in the path
Path cost: the sum of the costs of each edge 3.5 Chicago Seattle 2 2 Salt Lake City 2 2.5 2.5 2.5 3 San Francisco Dallas length(p) = 5 cost(p) = 11.5

15 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 if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices Can a directed graph which is strongly connected be acyclic? NO. (Except the trivial one node case.) There must be a path from A to B and a path from B to A; so, there must be a cycle. What about a weakly connected directed graph? YES! See the one on the slide. There are also further definitions of these; for example, the concept of biconnectivity showed up in my satisfiability research: Does the graph have two distinct paths between any two vertices.

16 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 C D E F Each of the red areas breaks one of these constraints. We won’t always require the constraint that the tree be directed. In fact, if it’s not directed, we can just pick a root and hang everything from that node (making the edges directed); so, it’s not a big deal. G H BAD! I J

17 Directed Acyclic Graphs (DAGs)
DAGs are directed graphs with no cycles. main() mult() if program call graph is a DAG, then all procedure calls can be in-lined add() DAGs are a very common representation for dependence graphs. The DAG here shows the non-recursive call-graph from a program. Remember topological sort? Any DAG can be topo-sorted. Any graph that’s not a DAG cannot be topo-sorted. read() access() Trees  DAGs  Graphs

18 Application of DAGs: Representing Partial Orders
reserve flight check in airport call taxi pack bags take flight Not all orderings are total orderings, however. Now, everyone tell me where there should be an edge! locate gate taxi to airport

19 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. reserve flight check in airport call taxi take flight taxi to airport locate gate pack bags

20 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 That’s a bit less efficient than we can do. Let’s try this. How well does this run? runtime:

21 Topo-Sort Take Two Label each vertex’s in-degree
Initialize a queue (or stack) to contain all in-degree zero vertices While there are vertices remaining in the queue Remove 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 Why use a queue? runtime:

22 Recall: Tree Traversals
i d h j b f k l e c g a b f g k c d h i l j e

23 Depth-First Search Both Pre-Order and Post-Order traversals are examples of depth-first search nodes are visited deeply on the left-most branches before any nodes are visited on the right-most branches visiting the right branches deeply before the left would still be depth-first! Crucial idea is “go deep first!” In DFS the nodes “being worked on” are kept on a stack (where?) Recursion is a clue that DFS may be lurking…

24 Level-Order Tree Traversal
Consider task of traversing tree level by level from top to bottom (alphabetic order) Is this also DFS? a i d h j b f k l e c g

25 Breadth-First Search No! Level-order traversal is an example of Breadth-First Search BFS characteristics Nodes being worked on maintained in a FIFO Queue, not a stack Iterative style procedures often easier to design than recursive procedures Put root in a Queue Repeat until Queue is empty: Dequeue a node Process it Add it’s children to queue

26 QUEUE a i d h j b f k l e c g a b c d e c d e f g d e f g e f g h i j
h i j k i j k j k l k l l a i d h j b f k l e c g

27 Graph Traversals Depth first search and breadth first search also work for arbitrary (directed or undirected) graphs Must mark visited vertices so you do not go into an infinite loop! Either can be used to determine connectivity: Is there a path between two given vertices? Is the graph (weakly) connected? Important difference: Breadth-first search always finds a shortest path from the start vertex to any other (for unweighted graphs) Depth first search may not!

28 Demos DFS BFS

29 Single Source, Shortest Path for Weighted Graphs
Given a graph G = (V, E) with edge costs c(e), and a vertex s  V, find the shortest (lowest cost) path from s to every vertex in V Graph may be directed or undirected Graph may or may not contain cycles Weights may be all positive or not What is the problem if graph contains cycles whose total cost is negative? The problem that Dijkstra’s algorithm addresses is the single source, shortest path problem. Given a graph and a source vertex, find the shortest path from the source to every vertex. We can put a variety of limitations or spins on the problem. We’ll focus on weighted graphs with no negative weights. This is used in all sorts of optimization problems: minimum delay in a network, minimum cost flights for airplane routes, etc.

30 The Trouble with Negative Weighted Cycles
2 A B 10 -5 1 E You might wonder why we don’t allow negative weights. Here’s one reason, we’ll see another later. The shortest path here is undefined! We can always go once more around the cycle and get a lower cost path. 2 C D

31 Edsger Wybe Dijkstra Legendary figure in computer science; now a professor at University of Texas. Supports teaching introductory computer courses without computers (pencil and paper programming) Also famout for refusing to read ; his staff has to print out messages and put them in his mailbox. To move to weighted graphs, we appeal to the mighty power of Dijkstra. This is one of those names in computer science you should just know! Like Turing or Knuth. So, here’s the super-brief bio of Dijkstra. Look him up if you’re interested in more.

32 Dijkstra’s Algorithm for Single Source Shortest Path
Classic algorithm for solving shortest path in weighted graphs (with only positive edge weights) Similar to breadth-first search, but uses a priority queue instead of a FIFO queue: Always select (expand) the vertex that has a lowest-cost path to the start vertex a kind of “greedy” algorithm Correctly handles the case where the lowest-cost (shortest) path to a vertex is not the one with fewest edges Among the wide variety of things he has done, he created Dijkstra’s algorithm more than 30 years ago. Dijkstra’s algorithm is a greedy algorithm (like Huffman encoding), so it just makes the best local choice at each step. The choice it makes is which shortest path to declare known next. It starts by declaring the start node known to have a shortest path of length 0. Then, it updates neighboring node’s path costs according to the start node’s cost. Then, it just keeps picking the next shortest path and fixing that one until it has all the vertices.

33 Pseudocode for Dijkstra
Initialize the cost of each vertex to  cost[s] = 0; heap.insert(s); While (! heap.empty()) n = heap.deleteMin() For (each vertex a which is adjacent to n along edge e) if (cost[n] + edge_cost[e] < cost[a]) then cost [a] = cost[n] + edge_cost[e] previous_on_path_to[a] = n; if (a is in the heap) then heap.decreaseKey(a) else heap.insert(a)

34 Important Features Once a vertex is removed from the head, the cost of the shortest path to that node is known While a vertex is still in the heap, another shorter path to it might still be found The shortest path itself from s to any node a can be found by following the pointers stored in previous_on_path_to[a]

35 Dijkstra’s Algorithm in Action
B D F H G E 2 3 1 4 10 8 9 7 OK, let’s do this a bit more carefully.

36 Demo Dijkstra’s

37 Data Structures for Dijkstra’s Algorithm
|V| times: Select the unknown node with the lowest cost findMin/deleteMin O(log |V|) |E| times: a’s cost = min(a’s old cost, …) What data structures do we use to support these little snippets from Dijkstra’s? Priority Queue and a (VERY SIMPLE) dictionary. Initialization just takes O(|V|), so all the runtime is in these pq operations. O(E log V + V log V)… but, if we assume the graph is connected, this becomes: O(E log V) (Dijkstra’s will work fine with a disconnected graph, however.) decreaseKey O(log |V|) runtime: O(|E| log |V|)

38 Fibonacci Heaps A complex version of heaps - Weiss 11.4
Used more in theory than in practice Amortized O(1) time bound for decreaseKey O(log n) time for deleteMin Dijkstra’s uses |V| deleteMins and |E| decreaseKeys So let’s compare the two. Binary heaps give us O(E log V) Fibonacci gives us O(|E| + V log V) So, if E = omega(V), this is asymptotically better. But, Fibonacci heaps have a good deal of overhead and are pointer-based. Therefore, binary heaps may be the right way to go. P.S. In dense graphs, unsorted linked-list queue is as fast as fibonacci heaps! runtime with Fibonacci heaps: O(|E| + |V| log |V|) for dense graphs, asymptotically better than O(|E| log |V|)


Download ppt "CSE 326: Data Structures Lecture #16 Graphs I: DFS & BFS"

Similar presentations


Ads by Google