0% found this document useful (0 votes)
2 views62 pages

Lecture 19

The document discusses graph algorithms, including definitions, properties, representations, and traversal techniques such as Breadth-First Search (BFS) and Depth-First Search (DFS). It explains how graphs can model relationships between objects and presents methods for representing graphs in computer memory. Additionally, it covers concepts like Hamiltonian and Eulerian cycles, as well as applications of DFS in topological sorting and detecting cycles.

Uploaded by

18-QADEER AHMAD
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)
2 views62 pages

Lecture 19

The document discusses graph algorithms, including definitions, properties, representations, and traversal techniques such as Breadth-First Search (BFS) and Depth-First Search (DFS). It explains how graphs can model relationships between objects and presents methods for representing graphs in computer memory. Additionally, it covers concepts like Hamiltonian and Eulerian cycles, as well as applications of DFS in topological sorting and detecting cycles.

Uploaded by

18-QADEER AHMAD
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/ 62

Algorithms

Lecture 19
In the Last Lecture
O Greedy Algorithms
O Huffman Coding
Graph
Algorithms
Graph Algorithm
O Definition
O Some Properties
O Graph Representation
O Graph Traversal
O BFS
O DFS
Introduction
O Any time there is a set of objects
and there is some sort of
“connection” or relationship or
interaction between pairs of
objects
O Graph is a good way to model
this
O Computer and communication
networks
O Transportation networks
Introduction
O VLSI, logic circuits
O Surface meshes for shape
description in computer aided
design and GIS
O Precedence constraints
O A graph G=(V,E) consists of:
O A finite set of vertices v(or nodes)
O E, a binary relation u called edges
O If set of pairs are ordered we have
directed graph or digraph
undirected graph digraph

1 2
1 2

3 4 3 4

1
G=(V,E)
V={1,2,3,4}
3 4
E={(1,2),(2,1),(2,4),(4,2)..}
Self loop
Adjacent Vertices
O A vertex w is adjacent to vertex v if
there is an edge from v to w
O The number of edges coming out of a
vertex is called the out-degree of that
vertex

1 2 1 and 2
1 and 3
1 and 4
3 4 3 and 4
Graph Properties
O The number of edges coming in is
the in-degree of that vertex
O A path in a un-direct graph is a
sequence of vertices and length of
the path is number of edges
O A vertex w is reachable from
vertex u if there is a path from v
to w
O A cycle in a digraph is a path
containing at least one edge
Graph Properties
O Hamiltonian cycle is a cycle that visits
every vertex of in a graph exactly once
O An Eulerian cycle is a cycle that visits
every edge of graph exactly once

1-2-4-3-1
1 2
1-4-3-1
1-3-1

3 4
Graph Properties
O A graph is said to be acyclic if it
contains no cycles
O A graph is connected if every vertex
can reach every other vertex
O A directed graph that is acyclic is
called a direct acyclic graph(DAG)
O A weighted graph is a triple(V,E,W)
W(e) is called the weight of e
Graph Representation
O We use two methods to
represent graphs on computer
i. Adjacency-list
representation
ii. Adjacency-matrix
representation
O Adjacency-list consist of an
array of |V| lists, one for each
vertex
O Adjacency-list of a graph
requires Θ(V+E) of memory
Adjacency-list representation
1 2

5 4

1 2 5 /

2 1 3 4 /
3 2 4 /
4 2 5 3 /

5 4 1
Adjacency-list representation
1 2 3

4 5 6

1 2 4 /

2 5 /
3 6 5 /
4 2 /

5 4 /

6 6 /
Adjacency-matrix representation

OAdjacency-matrix is a |V|*|V|
OA=(aij) such that:
1 if(i,j)ε E
aij=
0 otherwise

OAdjacency-matrix of a graph
requires Θ(V2) of memory
Adjacency-matrix representation

1 2 3 4 5
1 2
1 0 1 0 0 1
3
2 1 0 1 1 1
5 4
3 0 1 0 1 0
4 0 1 1 0 1
5 1 1 0 1 0
Adjacency-matrix representation
1 2 3 4 5 6

1 2 3
1 0 1 0 1 0 0
2 0 0 0 0 1 0
4 5 6 3 0 0 0 0 1 1
4 0 1 0 0 0 0
5 0 0 0 1 0 0
6 0 0 0 0 0 1
Which is better?
O It depends on the type of
application
O Adjacency-list saves the
memory
O But it is difficult to determine
whether there is a path
between two nodes or not
O This aspect can be easily
covered in adjacency-matrix
but it suffers as far as space is
concerned
Graph Traversal
O In general, there is no natural “first”
node in graph as root in tree
O There is no natural order among
successors of a particular node
O Unlike, tree a node in graph may have
more than one predecessor
O Two traversal techniques are used
i. Breath first Search
ii. Depth first Search
Graph Traversal
O For traversal we have to define a
source vertex s
O For each vertex vεV we will store the
d[v] which is the distance from s to v
note that d[s]=0
O We will also store a predecessor(or
parent) ∏[v] which is the first vertex
along this path if we walk from v
backward to s .We will also set
∏[s]=0
Breath First Search(BFS)

O Start with s and visit its adjacent


nodes
O Label the distance 1
O Now consider the neighbors of the
neighbors of s. They would be at
distance 2.
O Consider the neighbors of the
neighbors of the neighbors of s and
give them number 3
O Repeat this process until no more
unvisited neighbor left
Breath First Search(BFS)
O BFS computes the distance(smallest
no. of edges from s to each vertex)
O It produces a breath first tree with
a root s that contain all reachable
vertices
O To keep track of the progress, BFS
colors each vertex white, gray and
then black
O Color of each vertex is stored
color[u]
O BFS(G,s)
1. for each vertex u ε V[G]-
{s}
2. do color[u]  WHITE
3. d[u] ∞
4. ∏[u] Nil
5. color[s]GRAY
6. d[s] 0
7. ∏[s] Nil
8. QΦ
9. Enqueue(Q,s)
10.While Q≠Φ
11. do uDequeue(Q)
12. for each v ε Adj[u]
13. do if color[v]=WHITE
14. then color[v] GRAY
15. d[v] d[u]+1
16. ∏[v]u
17. Enqueue(Q,v)
18. color[u]BLUE
r s t u

(a)

v w x y

r s t u
∞ ∞ ∞
Q Φ
(b)
∞ ∞ ∞ ∞
v w x y
r s t u
∞ 0 ∞ ∞
Q s Φ
(c)
0
∞ ∞ ∞ ∞
v w x y

r s t u
1 0 ∞ ∞

w r Φ Q
(d)
1 1
∞ 1 ∞ ∞
v w x y
r s t u
1 0 2 ∞
Q r t x Φ
(e)
1 2 2
∞ 1 2 ∞
v w x y

r s t u
1 0 2 ∞

(f) Q t x v Φ

2 2 2
2 1 2 ∞
v w x y
r s t u
1 0 2 3

(g) Q x v u Φ

2 1 2 ∞ 2 2 3
v w x y

r s t u
1 0 2 3

(h) Q v u y Φ

2 2 3
2 1 2 3
v w x y
r s t u
1 0 2 3

(i) Q u y Φ

2 3
2 1 2 3
v w x y

r s t u
1 0 2 3

(j) Q y Φ

3
2 1 2 3
v w x y
r s t u
1 0 2 3

Q Φ
(k)
2 1 2 3
v w x y
Analysis
O BFS requires Θ (V+E) time to
complete its process
O Each item is only once enqueued
and dequeued
Properties of BFS
O BFS creates a tree during it
traversal
O It calculates shortest path from
source to any other node if the
graph is un-weighted
Depth-first Search
O The technique we use is called time-
stamp structure
O As we traverse the graph in DFS
order we will associate two numbers
with each vertex
O When we first visit a vertex v, store a
counter in d[v]
O When we finish processing a vertex
we store a counter in f[u]
O These two numbers are stamps
d e

b a f

c g
d e

b a f

1/..

c g
d e

DFS-VISIT(a)
DFS-VISIT(b)
DFS-VISIT(c)
b a f

1/..
2/.. 1/..

c g

3/..
d e

DFS-VISIT(a)
DFS-VISIT(b)
b a f DFS-VISIT(c)

2/5 1/..
2/5 1/..
c g

3/4
3/4
d e

DFS-VISIT(a)
DFS-VISIT(f)
b a f
DFS-VISIT(g)
2/5 1/.. 6/..

c g

3/4 7/..
d e

DFS-VISIT(a)

b a f DFS-VISIT(f)
DFS-VISIT(g)
2/5 1/.. 6/9

c g

3/4 7/8
d e

b a f

2/5 1/10 6/9

c g

3/4 7/8
d e

11/.. 12/..
DFS-VISIT(d)

b a f DFS-VISIT(e)

2/5 1/10 6/9

c g

3/4 7/8
d e

11/.. 12/13
DFS-VISIT(d)

f DFS-VISIT(e)
b a

2/5 1/10 6/9

c g

3/4 7/8
Properties of DFS
OEdge Classification
OParenthesis
Structure/Lemma
Edge Classification
O For a directed graph, the non tree
edges can be classified as follows:
O Back edge:(u,v) where v is an
ancestor of u in the tree
O Forward edge:(u,v) where v is
proper descendent of u in the tree
O Cross edge:(u,v) where u and v
are disjoint.no ancestor and
decendent relationship
d e

11/14 12/13

C C
b a f

2/5 1/10 6/9

F B
c g

3/4 7/8
c
DFS Structure
O The time stamp given by the
DFS allow us to determine a
number of things about a graph
or digraph
O For example, we can determine
whether the graph contains any
cycle
O If there is any back edge in the
graph than it contains cycle
O Beware there is no relationship
between the no. of back edges
and no. of cycles
Parenthesis Structure
O If we represent (v as starting
time of vertex v and v) as
ending time
O DFS form a full parenthesized
structure
O Time of u is overlapping for v
OR
O Time of u and v are disjoint
Applications of DFS
OTopological Sort
OStrongly Connected
Components
Topological Sort
O A directed acyclic graph(DAG) arise
in many applications where there
are precedence or ordering
O There are series of tasks to be
performed and certain tasks must
precede other tasks
O In general, a precedence constraints
graph is a DAG in which vertices are
tasks and the edges(u,v) means
that task u must be completed
before task v begins
Topological Sort
O A topological sort of a DAG is a
linear ordering of the vertices of
the DAG such that for each
edge(u,v) u appear before v in the
ordering
Computer Programming Discrete Math

Philosophy

Data structure D&A

Compiler Construction Theory of automata

Artificial Intelligence

Speech Recognition
11/16 17/18

Computer Programming Discrete Math 9/10

Philosophy
12/15

Data structure D&A 13/14

6/7 1/8

Compiler Construction Theory of automata

2/5

Artificial Intelligence

3/4

Speech Recognition
17/18 11/16 13/14
12/15
Discrete Math CP DS D&A

1/8 6/7
9/10
Philosophy Automata CC

2/5 3/4
Artificial Intelligence Speech Recognition
Topological Sort
O TOPOLOGICAL-SORT(G)
1. Call DFS(G) to compute finishing
times f[v] for each vertex
2. As each vertex is finished ,insert
it onto the front of a linked list
3. Return the linked list of vertices
We can perform topological sort in
(V+E) time since DFS takes
(V+E) and it takes (1) time to
insert |V| vertices on to the front
of the link list
Strongly Connected
Components
O Now, we consider an important
connectivity with digraph
O When digraphs are used in
communications and
transportations networks
people want to know there
networks are complete
O Complete in sense it is possible
to from any location to reach
any other location in a digraph
Strong Connection
O A digraph is strongly connected if
for every pair of vertices u,vεV u
can reach v and vice-versa
O We will like to write an algorithm
that determines whether a digraph
is strongly connected or not
O In fact, we will solve a
generalization problem of this
problem, of computing the strongly
connected components of a digraph
A

B C

D G I

E F H
A

B C

D G I

E F H
A,B,C

D,E

F,G,H,I

Component DAG
Algorithm
Graph Transpose
O Graph transpose of a G=(V, E) is
O GT=(V,ET)

A A

C B C
B

You might also like