0% found this document useful (0 votes)
11 views19 pages

L17 Elementary Graph Algo

Chapter 22 covers elementary graph algorithms, including definitions, representations, and search algorithms like breadth-first search (BFS) and depth-first search (DFS). It explains graph representations using adjacency lists and matrices, detailing their advantages and disadvantages based on graph density. The chapter also provides a comprehensive overview of the BFS algorithm, its runtime analysis, and its application in finding the shortest path in graphs.

Uploaded by

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

L17 Elementary Graph Algo

Chapter 22 covers elementary graph algorithms, including definitions, representations, and search algorithms like breadth-first search (BFS) and depth-first search (DFS). It explains graph representations using adjacency lists and matrices, detailing their advantages and disadvantages based on graph density. The chapter also provides a comprehensive overview of the BFS algorithm, its runtime analysis, and its application in finding the shortest path in graphs.

Uploaded by

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

Chapter 22: Elementary Graph Algorithms

Overview:
Definition of a graph
Representation of graphs
adjacency list
matrix
Elementary search algorithms
breadth-first search (BFS)
depth-first search(DFS)
topological sort
strongly connected components
Chapter 22 part 1: Fundamentals and Breadth-First search
Notation

Graph G(V,E) is a data structure defined by


a set of vertices V
a set of eges E

|V| = # of vertices

|E| = # of edges

Usually omit | | in asymptotic notation


Q(V, E) means Q(|V|, |E|)
Adj = adjacency-list representation of G(V,E) is an array of |V| lists

Adj[u] contains all vertices adjacent (connected by edges) to vertex u

For a directed graph, edge (uv) represented by v in Adj[u].


 sum of lengths of adjacency lists = |E|

For an undirected graph, edge (u,v) appears as v in Adj[u]


and as u in Adj[v].
\sum of lengths of adjacency lists = 2|E|

Memory requirement is Q(V+E)

A weighted graph has a function w(u,v) defined on the domain E.

The weight of edge (u,v) can be stored as property of vertex v in Adj[u].


adjacency-matrix representation:
Number the vertices 1,2,...|V|

Define |V| x |V| matrix A such that aij = 1 if (i, j)  E,


aij = 0 otherwise

For a weighted graph, set aij = w(i,j) if (i, j)  E

Disadvantage is requires Q(V2) memory regardless of |E|

Advantage is speed of determining if edge (u,v) is in graph

For an undirected graph A is symmetric

A sparse graphs mean |E| << |V|2


adjacency-list representation preferred (saves space)

A dense graph means |E| ~ |V|2


adjacency-matrix representation preferred (speed)
Graph representations

1
Breadth-first search: Given G(V,E) and a source vertex s,

Breadth-first search does the following:


(1) finds every vertex v reachable from s

(2) calculates distance (minimum number of edges)


between s and v

(3) produces a Breadth-first tree with s as the root and


every reachable vertex as a node

The path from s to v in the Breadth-first tree corresponds to the


shortest path between s and v in G.

“Breadth-first” finds all vertices at distance k from s before


searching for any vertices at distance k+1.
BFS(G,s) pseudo code
for each u  V(G) – {s}
do color[u]  white, d[u]  , p[u]  NIL

(Initialization: color all vertices white for “undiscovered”


set distance from s as infinite,
set predecessor in Breadth-first tree to NIL)

color[s]  gray, d[s]  0, p[s]  NIL

(initialize s as gray for “discovered” i.e. reachable from s


A gray vertex has been discovered but its adjacency list
has not been searched for links to other vertices.
After adjacency list is searched, color is changed to black)

Q  0, Enqueue(Q,s)

(A “first-in first-out” queue holds a list of the current gray vertices


Gray vertices with the smallest distance from s are processed first
BFS(G,s) algorithm continued
while Q  0
do u  Dequeue(Q)
for each v  Adj[u]
do if color[v] = white then
color[v] = gray
d[v]d[u] + 1
p[v] u
Enqueue(Q,v)
color[u]  black (adj list searched)

Runtime analysis:
initialization requires O(V).
each vertex is discovered at most once
 queue operations require O(V).
searching adjacency lists requires O(E)
 total runtime O(V+E)
Example of BFS (p596) using pseudo-code
Example of BFS (p596) using pseudo-code

How would this result be different


if x was dequeued before t?
Predecessor of v:
p(v) is the vertex in whose
adjacency list v was discovered

Predecessor sub-graph
Gp(Vp,Ep)
Vp = {v  V : p[v]  NIL}  {s}
Ep = {(p[v],v) : v  Vp - {s}}

Predecessor sub-graph of BFS is a


single tree

How would this tree be different if


x was dequeued before t?
BFS by hand:
Unnecessary to perform all steps of pseudo-code
Find all vertices at distance k from s before searching for any vertices at distance k+1.
Resolve ambiguities about which adjacency list to search next by alphabetic order
Mark the edges used in a search with a T for tree edge. Keep a record of parentage.
Simple example

s t

v u
BFS by hand:
When a new vertex is discovered enter number one greater than the parent.
Resolve ambiguities about which adjacency list to search next by alphabetic order
Example: u can be discovered from either t or v. Choose t
Mark the edges used in a search with a T for tree edge. Keep a record of parentage.

s t
T
0 1

v u
BFS by hand:
With ambiguity resolved by alphabetical order, edge (t,u) becomes a
tree edge rather than (v,u)

s t
T
0 1

T T

1 2

v u
Finding shortest path is most common application of
Breadth-first search
If a vertex is reachable from the source, the integer in each vertex, d(s,a),
is the length in edges of the shortest path from the source.

d(s,a) =  if a is not reachable from s

s t
T
0 1

T T

1 2

v u
Weight of a path

Weight of a path is the sum of the weights of its edges

For an unweighted graph,


weight of path = number of edges = length of path

For weighted graph, least-weight is not necessarily shortest path


Theorem 22.5: On termination of BFS(G,s),
(1) all reachable vertices have been found

(2) d[v] = d(s,v) for v  V (some may be infinite)

(3) for any reachable v  s, a shortest path includes


p[v] followed by edge (p[v],v).
Assignment 16
Ex 22.2-2 text p 601. Perform BFS. Use alphabetical order to resolve ambiguity
in order of discovery. Mark tree edges. Make a table of p[v] for all v. Draw Gp
Note: u is the source vertex.

You might also like