Can you get there from here? Graphs and Searches Can you get there from here?
Graph A graph G is a set of vertices V = {v1, v2,…, vn} and a set E of pairs of vertices known as edges E = {(vi, vj): 1 ≤ i, j ≤ n} Graphs may be represented pictorially 3 1 2 7 4 6 5 9 8 12/6/2018 DRAFT
Graph Graphs may be represented computationally Adjacency lists Adjacency matrices V Is adjacent to 1 4, 5 2 6 3 6, 7 4 1, 8 5 2, 3, 8, 9 7 3, 9 8 4, 5, 6, 9 9 6, 7, 8 3 1 2 7 4 6 5 9 8 12/6/2018 DRAFT
Breadth First Search Bfs(v) visited[v] = 1 u = v Create an empty queue Q Loop For all vertices w that are adjacent to u If visited[w] = 0 Add w to Q Mark w as visited, visited[w]=1 If Q is empty, return Remove head of Q and place in u Repeat 12/6/2018 DRAFT
Depth First Search Dfs(v) Example Bfs and Dfs code link visited[v] = 1 For each vertex w adjacent to v If visited[w] == 0, then Dfs(w) Example Bfs and Dfs code link 12/6/2018 DRAFT