{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# T10 - Graphs(BFS DFS COMP171 Tutorial 10 Graphs Graph...

This preview shows pages 1–9. Sign up to view the full content.

Graphs (BFS & DFS) COMP171 Tutorial 10

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Graphs Graph G=(V,E) V: set of vertices E: set of edges V={1,2,3,4,5} E={(1,2)(1,5)(2,5)(2,4)(4,5)(2,3)(2,4)} Two standard ways to represent a graph As a collection of adjacency lists As an adjacency matrix
Adjacency List An array Adj of |V| lists For each u in V, the adjacency list Adj [u] contains all the vertices v such that (u,v) in E

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Adjacency Matrix A matrix A=(a i,j ), where a i,j =1 if (i,j) is in E a i,j =0 if (i,j) is not in E Size of the matrix is |V|*|V|
Comparison Adjacency List is usually preferred, because it provides a compact way to represent sparse graphs – those for which |E| is much less than |V| 2 Adjacency Matrix may be preferred when the graph is dense , or when we need to be able to tell quickly if their is an edge connecting two given vertices

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Graph Traversal How to visit all vertices in a graph in some systematic order? - Graph traversal may start at an arbitrary vertex. (Tree traversal generally starts at root vertex.) - Two difficulties in graph traversal, but not in tree traversal: - The graph may contain cycles; - The graph may not be connected. - There are two important traversal methods: - Breadth-first traversal, based on breadth-first search (BFS). - Depth-first traversal, based on depth-first search (DFS).