Graph Traversal
Total Page:16
File Type:pdf, Size:1020Kb
CSE 373: Graph traversal Michael Lee Friday, Feb 16, 2018 1 Solution: This algorithm is known as the 2-color algorithm. We can solve it by using any graph traversal algorithm, and alternating colors as we go from node to node. Warmup Warmup Given a graph, assign each node one of two colors such that no two adjacent vertices have the same color. (If it’s impossible to color the graph this way, your algorithm should say so). 2 Warmup Warmup Given a graph, assign each node one of two colors such that no two adjacent vertices have the same color. (If it’s impossible to color the graph this way, your algorithm should say so). Solution: This algorithm is known as the 2-color algorithm. We can solve it by using any graph traversal algorithm, and alternating colors as we go from node to node. 2 Problem: What if we want to traverse graphs following the edges? For example, can we... I Traverse a graph to find if there’s a connection from one node to another? I Determine if we can start from our node and touch every other node? I Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-first search and depth-first search Goal: How do we traverse graphs? Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them 3 I Determine if we can start from our node and touch every other node? I Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-first search and depth-first search Goal: How do we traverse graphs? Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... I Traverse a graph to find if there’s a connection from one node to another? 3 I Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-first search and depth-first search Goal: How do we traverse graphs? Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... I Traverse a graph to find if there’s a connection from one node to another? I Determine if we can start from our node and touch every other node? 3 Solution: Use graph traversal algorithms like breadth-first search and depth-first search Goal: How do we traverse graphs? Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... I Traverse a graph to find if there’s a connection from one node to another? I Determine if we can start from our node and touch every other node? I Find the shortest path between two nodes? 3 Solution: Use graph traversal algorithms like breadth-first search and depth-first search Goal: How do we traverse graphs? Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... I Traverse a graph to find if there’s a connection from one node to another? I Determine if we can start from our node and touch every other node? I Find the shortest path between two nodes? 3 Goal: How do we traverse graphs? Today’s goal: how do we traverse graphs? Idea 1: Just get a list of the vertices and loop over them Problem: What if we want to traverse graphs following the edges? For example, can we... I Traverse a graph to find if there’s a connection from one node to another? I Determine if we can start from our node and touch every other node? I Find the shortest path between two nodes? Solution: Use graph traversal algorithms like breadth-first search and depth-first search 3 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: Queue: a, Visited: a, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: a Queue: Visited: a, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: a Queue: b, d, Visited: a, b, d, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: b Queue: d, Visited: a, b, d, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: b Queue: d, c, e, Visited: a, b, d, c, e, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: d Queue: c, e, Visited: a, b, d, c, e, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: d Queue: c, e, f, g, Visited: a, b, d, c, e, f, g, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: c Queue: e, f, g, Visited: a, b, d, c, e, f, g, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: e Queue: f, g, Visited: a, b, d, c, e, f, g, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: e Queue: f, g, h, Visited: a, b, d, c, e, f, g, h, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: f Queue: g, h, Visited: a, b, d, c, e, f, g, h, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: g Queue: h, Visited: a, b, d, c, e, f, g, h, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: g Queue: h, i, Visited: a, b, d, c, e, f, g, h, i, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: h Queue: i, Visited: a, b, d, c, e, f, g, h, i, 4 Breadth-first search (BFS) example search(v): visited = empty set f queue.enqueue(v) visited.add(v) g d a j while (queue is not empty): i curr = queue.dequeue() h for (w : v.neighbors()): e b if (w not in visited): queue.enqueue(w) visited.add(curr) c Current node: i Queue: Visited: a, b, d, c, e, f, g, h, i, 4 2.