This preview shows pages 1–7. Sign up to view the full content.
Graphs
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
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:

Breadthfirst traversal, based on breadthfirst search (BFS).

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.
 Spring '08
 WOO

Click to edit the document details