{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Graphs (BFS & DFS) COMP171 Tutorial 10
Background image of page 1

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

View Full Document Right Arrow Icon
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
Background image of page 2
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
Background image of page 3

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

View Full Document Right Arrow Icon
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|
Background image of page 4
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
Background image of page 5

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

View Full Document Right Arrow Icon
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).
Background image of page 6
Breadth-First Search (BFS)
Background image of page 7

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

View Full Document Right Arrow Icon
Breadth-First Search Traversal Breadth-first traversal of a graph: -
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}