0% found this document useful (0 votes)
79 views6 pages

Adjacency List Representation: Episode 38

This document discusses graphs and graph traversal algorithms. It begins with an overview of graph representations using adjacency lists and matrices. It then covers the depth-first search (DFS) and breadth-first search (BFS) algorithms for traversing graphs. DFS uses a stack and visits neighbor nodes in a recursive, depth-first manner. BFS uses a queue and visits neighbors in levels, providing the shortest path between nodes. Both algorithms run in time linear to the number of edges and vertices.

Uploaded by

Pervaz Mohammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views6 pages

Adjacency List Representation: Episode 38

This document discusses graphs and graph traversal algorithms. It begins with an overview of graph representations using adjacency lists and matrices. It then covers the depth-first search (DFS) and breadth-first search (BFS) algorithms for traversing graphs. DFS uses a stack and visits neighbor nodes in a recursive, depth-first manner. BFS uses a queue and visits neighbors in levels, providing the shortest path between nodes. Both algorithms run in time linear to the number of edges and vertices.

Uploaded by

Pervaz Mohammad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Episode 38: Graphs and Graph Traversal

rev. 28/03/2009 23:56

Reading: First read two pages about graphs in the Math Primer lecture.
GT 6.1.0, 6.1, 6.2 (skip 6.2.3), 6.3, p.289316 [ca. 25pp, approx. 2h]
This note tries to be complete: it contains both material already explained in
FAAP and some new stuff. After a brief remembering of some elements from
FAAP lecture, we will focus on complexity issues in graph algorithms.
Ingredients: Graphs as mathematical objects, Adjacency List representation,
DFS, BFS

Adjacency List Representation


For each vertex we keep a linked list of vertices that are adjacent to it. Uses
space proportional to |E| + |V | (so proportional to the size of the graph).
6

2
1
3
5

Figure 1: A graph and its adjacency list representation


Q. What if the worst-case cost of a single adjacency check?
Q. What is the cost of iterating over all neighbours of a given node?

Adjacency Matrix Representation


The adjacency list representation is most suitable for representing sparse graphs,
i.e. graphs were the number of edges is much less than |V |2 . Sparse graphs often appear in modeling natural (physical) problems, like connections between
people on Facebook or road maps (like Google maps).
1

Algorithms and Data Structures

For dense graphs, with |E| being close to 12 |V |2 , the adjacency matrix is a more
suitable representation. Adjacency matrix is a two dimensional array, say adj ,
such that adj [v][w] = 1 if there is an edge v w (and adj [v][w] = 0 otherwise). Dense graphs sometimes appear in scientific applications.
0

Figure 2: An adjacency matrix for the graph of Fig. 1


If there is data associated with edges (for example weights) it can be naturally
referenced from the matrix, instead of using zeros and ones.
Adjacency matrix always uses space proportional to |V |2 , regardless how many
edges are in the represented graph.
A single adjacency test takes constant time with this representation.
Iteration over neighbors of a given node takes O(|V |) timeno longer linear
in the number of the neighbors.

Depth-First-Search
Searching is among the most common operations on graphs. It is sometimes
also called traversing graphs or visiting (e.g. like in visitor pattern).
D FS -V ISIT(v :vertex)
1
2
3
4

if v not previously visited


then record that v has been visited
for every neighbor w of v
do D FS -V ISIT(w)

Algorithms and Data Structures

An example of running this algorithm can be found in Fig. 3


Q. What is the worst-case memory usage of D FS -V ISIT ?
Q. Does DFS find the shortest paths from start node to other nodes?
Theorem 1. DFS using an adjacency list requires time proportional to |E| + |V |, and
time proportional to |V |2 with adjacency matrix.

So the search takes time linear in the size of the structure.


For dense graphs |E| is proportional to |V |2 , for sparse graphs |V | is typically
proportional to |E|, so |E| is always the dominating parameter here.
Applications of DSF include cycle detection, connectivity testing, finding a path
between v and w in a graph, finding spanning trees / spanning-forests.

Breadth-First-Search (BFS)
Observe that DFS can be implemented using an explicit stack:

D FS -V ISIT(v : vertex)
1
2
3
4
5
6
7
8

P USH(v)
while stack not empty
do
v P OP
if v has been previously visited
then continue
Record that v has been visited
for each neighbor u of v do P USH(u)

If we use a FIFO Queue instead of a stack we obtain an algorithm called


Breadth First Search:

Algorithms and Data Structures

4
0

1
3

1
3

3
4

(a) stack: 0
0

(b) stack: 0 6
6

0
2

1
3

1
3

(d) stack: 0 6 4 3

3
4

(e) stack: 0 6 4 3 5
6

0
2

1
3

1
3

3
4

stack: 0 6 4
stack: 0 6 4 5
(g) stack: 0 6 4

(i) stack: 0

1
3

1
3

(j) stack: 0 2
0

(h) stack: 0 6

(f) stack: 0 6 4 3
6

(c) stack: 0 6 4
6

3
4

(k) stack: 0

(l) stack: 0 1

6
2
1
3

stack: 0
(m) stack: empty

Figure 3: An example of running DFS on the graph of Fig. 1, starting in node 0

Algorithms and Data Structures

B FS -V ISIT(v : vertex)
1
2
3
4
5
6
7
8

E NQUEUE(v)
while queue not empty
do
v D EQUEUE
if v has been previously visited
then continue
Record that v has been visited
for each neighbor u of v do E NQUEUE(u)

Example in Fig. 4. During the BFS traversal a spanning tree is constructed for
the graph. For exampl in Fig. 4(h) the spanning tree is highlighted in yellow.
This is because during BFS vertices leave the FIFO queue in order of their
distance from the start vertex.
Conclusion: BFS can be used to find a shortest path between two vertices
(shortest = with the minimum number of edges).
Theorem 2. BFS visits all the vertices and edges in a graph in time proportional to
|V | + |E| with the adjacency list, and in time proportional to |V |2 with the adjacency
matrix.

Algorithms and Data Structures

1
3

1
3

(a) queue 0
0

3
4

(b) queue 50 10 20 60
6

(c) queue 10 20 60 05 35 45
6

1
3

1
3

3
4

(d) queue 20 60 05 35 45 01

(e) queue 60 05 35 45 01 02

queue 05 35 45 01 02 06 46
(f) queue 35 45 01 02 06 46

1
3

3
4

(g) queue 45 01 02 06 46 53 43

(h) 01 02 06 46 53 43 54 34 64

Figure 4: An example of running BFS on the graph of Fig. 1, starting in node 0.


After the last state, the queue is emptied by a series of calls to D EQUEUE

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy