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

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

View Full Document Right Arrow Icon
Graphs COMP171 Tutorial 10
Background image of page 1

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

View Full DocumentRight 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 DocumentRight 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 DocumentRight Arrow Icon
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). -
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/22/2008 for the course CS 105 taught by Professor Woo during the Spring '08 term at HKUST.

Page1 / 23

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

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online