Week 9 - Friday
What did we talk about last time? Graph basics Graph ADT Adjacency matrix representation
Wednesdays at 5 p.m. in The Point 113 Saturdays at noon in The Point 113
An adjacency matrix wastes a lot of space if the graph is not very dense An alternative is an adjacency list The form of an adjacency list is an array of length n where the i th element is a pointer to a linked list (or dynamically allocated array) of the nodes adjacent to node i This is the approach the book focuses on, since most graphs are not dense
1 0 1 0 0 2 3 4 1 2 1 3 2 1 2 4 3 3 4 1 3 4
1 0 1 0 2 3 1 2 1 3 2 4 3 3 4 3 4
It’s a trick! 1 Some other steps must be 0 taken to represent a 2 multigraph with an adjacency list. 3 Each node in the linked list must contain additional 4 information.
Again, we need extra information in the lists. 1 5 0 5 1 2 0 2 2 0 5 3 6 4 9 1 2 1 2 3 3 2 6 9 3 3 1 6 2 3 4 4 3 4 1 9 3 4 4 4
Similar to a preorder traversal in a tree We want to visit every node once, going down as far as possible before backing up Issues: No guarantee about ordering like a BST Loops are a problem, how do we keep from repeating nodes?
We might start at an arbitrary 1 1 1 1 1 1 location. 0 0 0 0 0 0 What’s a DFS look 2 2 2 2 2 2 like that starts at node 4? 3 3 3 3 3 3 4, 1, 0, 2, 3 4 4 4 4 4 4
We use pseudocode a lot when describing graph operations, since the details depend on implementation choice Nodes all need some extra information, call it number Startup Set the number of all nodes to 0 1. Pick an arbitrary node u and run DFS( u , 1 ) 2. DFS( node v, int i ) Set number( v ) = i ++ 1. Do whatever other processing for v is necessary 2. For each node u adjacent to v 3. If number( u ) is 0 DFS( u , i )
What if we wanted to find paths from node s to other nodes? We run DFS starting at s with an extra array of length | V | called edges When we move from node u to node v, we set edges [ v ] = u Then, to find a path from s to t , we backtrack by looking at edges [ t ] and working backwards until we get to s This approach will find a path if there is one, but it may not be the shortest path
B B C C Edge Node To find paths from B From to other nodes, we A B A A first do a DFS from B B E E C D D D D A H H E C G G F G I I F F G E H G J J I Working backwards, a path from F to B is: F G E C D A B J Thus, a path from B to F is: B A D C E G F
Similar to a level order traversal in a tree We want to visit every node once, visiting all the children of one node before moving on to their children Similar issues to a DFS
We might start at an arbitrary 1 1 1 1 1 1 location. 0 0 0 0 0 0 What’s a BFS look 2 2 2 2 2 2 like that starts at node 4? 3 3 3 3 3 3 4, 1, 3, 0, 2 4 4 4 4 4 4
More pseudocode! Nodes all need some extra information, call it number BFS(node v ) Set the number of all nodes to 0 1. Create queue q 2. Set i = 1 3. number( v ) = i ++ 4. q .enqueue( v ) 5. While q is not empty 6. a. v = q .dequeue() b. Do whatever other processing for v is necessary c. For each node u adjacent to v If number( u ) is 0 Set number( u ) = i ++ q .enqueue( u )
Let V be the set of vertices and E be the set of edges Thus, | V | is the number of vertices and | E | is the number of edges If you are using adjacency lists then: DFS is: ▪ O(| V | + | E |) BFS is: ▪ O(| V | + | E |) If you are using an adjacency matrix then: DFS is: ▪ O(| V | 2 ) BFS is: ▪ O(| V | 2 )
We have spent a huge amount of time on trees in this class Trees have many useful properties What is the only difference between a tree and a graph? Cycles Well, technically a tree is also connected
Is a graph a tree? It might be hard to tell We need to come up with an algorithm for detecting any cycles within the possible tree What can we use? Depth First Search!
Nodes all need some extra information, call it number Startup Set the number of all nodes to 0 1. Pick an arbitrary node u and run Detect( u , 1 ) 2. Detect( node v, int i ) Set number( v ) = i ++ 1. For each node u adjacent to v 2. If number( u ) is 0 Detect( u , i ) Else Print “Cycle found!”
Even graphs with unconnected components can have cycles To be sure that there are no cycles, we need to run the algorithm on every starting node that hasn’t been visited yet
A directed acyclic graph (DAG) is a directed graph without cycles in it Well, obviously These can be used to represent dependencies between tasks An edge flows from the task that must be completed first to a task that must come after This is a good model for course sequencing Especially during advising A cycle in such a graph would mean there was a circular dependency By running topological sort, we discover if a directed graph has a cycle, as a side benefit
A topological sort gives an ordering of the tasks such that all tasks are completed in dependency ordering In other words, no task is attempted before its prerequisite tasks have been done There are usually multiple legal topological sorts for a given DAG
Give a topological sort for the following DAG: A F I C K G D J E H A F I C G K D J E H
Create list L Add all nodes with no incoming edges into set S While S is not empty Remove a node u from S Add u to L For each node v with an edge e from u to v ▪ Remove edge e from the graph ▪ If v has no other incoming edges, add v to S If the graph still has edges Print "Error! Graph has a cycle" Otherwise Return L
Connectivity Minimum spanning trees Shortest paths Matching on bipartite graphs Stable marriage
Work on Project 3 Finish Assignment 4 Due tonight! Read 4.3 and 4.4
Recommend
More recommend