####.##.## #....#..## #.####.#.# #......#.# #####....# ##########
--- template:dfs What order will the DFS traversal visit the nodes in the graph on the right when we start DFS at A? -- There is more than one possible traversal. The exact order depends on how the graph is stored and the order in which we access the nodes adjacent to a given node. Let's look at some possible traversals --- template:dfs - start with A (visited: A) -- - We have three choices for the next node: B, F, or H. Let's pick F (visited: A, F). -- - From F, we can go to D or H. Let's pick H (visited: A, F, H). -- - At H, the only choices are A and F, and both have been visited. We return to F and look for other options; the only one is D (visited: A, F, H, D). -- - From D, we can go to E, G, or C (since F has already been visited). Let's pick G (visited: A, F, H, D, G). -- - G has no unvisited neighbors, so we return to D. Let's pick E (visited: A, F, H, D, G, E). -- - E's only unvisited neighbor is B, so we go there (visited: A, F, H, D, G, E, B). -- - B's only unvisited neighbor is C, so we go there (visited: A, F, H, D, G, E, B, C). -- - C's only unvisited neighbor is J, so we go there (visited: A, F, H, D, G, E, B, C, J). -- At this point, we have visited all the nodes. Our traversal is: A, F, H, D, G, E, B, C, J An algorithm would still need to check whether all nodes have been visited. --- template: dfs Here are two other DFS traversals starting at node A: - A, B, E, D, F, H, G, C, J - A, B, C, J, D, E, F, H, G -- __Task__ List all possible traversals starting at node with label J. --- template:dfs An algorithm using the adjacency list `adj` of a graph with `n` nodes and `m` edges runs in `O(n + m)` time. .below-column2[ ``` visited[n] - boolean array, initially all false dfs ( start ) if visited[start] return //already processed this node visited[start] = true //mark before exploring neighbors visit/process the node for neighbor in adj[start] dfs( neighbor ) ``` ] --- name:bfs ## Breadth First Search Traversal .right-column2[ ] --- template:bfs - Visits nodes in order of their distance from the starting node, where distance is the length of the shortest path. - Visit all nodes whose distance from the start node is 1. - Then visit all nodes whose distance from the start node is 2. - ... -- What order will the BFS traversal visit the nodes in the graph on the right when we start BFS at A? -- Again, the exact order depends on how we store the graph and the order in which we access the nodes adjacent to a given node. Here are possible traversals for the graph on the right. - A, B, F, H, C, E, D, G, J -- __Question__ Which nodes at the same distance from A can be reordered without making this an invalid BFS traversal? Explain. -- __Task__ Propose a BFS traversal of this graph starting at node J. --- template:bfs An algorithm using the adjacency list `adj` of a graph with `n` nodes and `m` edges runs in `O(n + m)` time. ``` visited[n] - boolean array, initially all false queue - stores nodes to process bfs ( start ) visited [ start ] = true visit/process the start node queue.push( start ) while queue is not empty node = queue.dequeue() for neighbor in adj[node] if ! visited[neighbor] visited[neighbor] = true visit/process neighbor queue.push( neighbor ) ``` --- template:section # Examples and Things to Think About --- ## Graph Implementation Graphs serve many different purposes, so their implementations need to suit the task. Many programming languages' standard libraries do not provide a general-purpose graph data structure. Implement a graph. Pick any implementation you want (or try it with all of them). Assume that the graph nodes have integer labels from 1 to N. The graph is undirected and is described by a list of edges: `a b` indicates that there is an edge between `a` and `b`. -- How would your implementation differ for a directed graph? -- Modify your code to handle weighted graphs. In this case, each edge is described as `a b w`, indicating an edge from `a` to `b` with weight `w`. --- template: challenge ## Challenge Design algorithms for the following problems: - Given a pair of nodes, find the length of the shortest path between them. Solve this for unweighted and weighted graphs. - Given a node, find the length of the shortest path from it to every other node in the graph. Solve this for unweighted and weighted graphs. - Determine whether a graph is connected or disconnected (algorithmically, not visually). If it is disconnected, find the number of nodes in each connected component. - Determine whether a graph is a tree (algorithmically, not visually). --- ## Solving Problems Try using your graph implementation to solve the following problems: - [Ab Initio](https://open.kattis.com/problems/abinitio) - The description is long, but the problem is fairly approachable if you already have a graph implementation. - [Flying Safely](https://open.kattis.com/problems/flyingsafely) - [Cantina of Babel](https://open.kattis.com/problems/cantinaofbabel) - This problem is not difficult, but first you need to figure out how to model it as a graph.