Académique Documents
Professionnel Documents
Culture Documents
Chapter 7:
Graph
Imran Ali Memon
Lecturer IT Department
A
B C
D E
F
G
Linear Data Structures
Non-Linear Data Structure
7.1 Introduction
• A graph is a non-linear data structure.
• Graph is a collection of nodes V connected by edges E.
– Nodes also called vertices, singular is vertex
– Edges also called arcs
– Each node contains an element
– Each edge connects two nodes together (or possibly the same node
to itself) and may contain an edge attribute
Mathematically :
G=(V,E) (G is an ordered pair of V and E)
V(G): set of vertices
E(G): a set of edges
7.2 Types of Graphs
• There are two kinds of graphs:
1. Directed graphs (Unidirectional)
– Sometimes called digraphs
– Edges have a direction
– Warning: if the graph is directed, the
order of the vertices in each edge is
important !!
Unweighted
Graph
Weighted
Graph
Terminology: Labeled & Unlabeled Graph
• In labeled graph each
vertex is assigned a unique
name or identifier to
distinguish it from all other
vertices Unlabeled
Graph
Labeled
Graph
Terminology: Trees vs Graphs
• Trees are special cases of graphs!!
• Tree
– If N nodes then N-1 edges.
– 1 edge for each parent-child
– All nodes must be reachable from root
• Graph
– No rules for connection among nodes.
– Edges can connect nodes in any possible way a
a
e
b e
c
f b d
f d
Terminology: Complete Graph
b
A complete graph is a graph that has
the maximum number of edges a d
– for undirected graph with n vertices, the
maximum number of edges is n(n-1)/2 c
– 4(4-1)/2 = 4(3)/2 = 12/2 = 6
b
– for directed graph with n vertices, the
maximum
number of edges is n(n-1) a d
– 4(4-1) = 4(3) = 12
c
Terminology: Node(Vertex) & Edge (Arc)
Nodes
D is adjacent of A
D and A are endpoints of edge A is adjacent of D
Terminology: Degree (In-Deg & Out-Deg)
A
• The degree of a node is the number of B
edges it has. E
D F
– deg(E) = 3, deg(F) = 2 …
C
G
• For directed graphs, Undirected Graph
Directed Graph
Terminology: Source , Sink
• A node is called a source if it has A
B
outdegree but zero indegree.
E
– A & F are sources vertices
D
F
Directed Graph
Terminology: Isolated node
• If only one node in graph then, it is
called as isolated node. A
OR
• If deg(node) = 0 , it is called as
isolated node.
Terminology: Path A
E
A
E
D
The path P is said to be closed path if first nodes is last node.
< A, D, G, C, A > G
F
C
Terminology: Multiedges, Loop
If a graph contains multiedges or loop , then it is called
multigraph.
• If different edges connect the
f
same endpoints is called multi
edges. a
A E
– e = {A,D} , d = {A,D} e
d b
D F
• If an edge has identical endpoints c
is called loop.
– f = {E,E}
Exercise of terminologies
• Graph
• Multigraph
f
• loop f={E,E}
a
• multiedges = d={A,D} & e={A,D} A E
• 4 nodes / vertices e
d b
• 6 edges / arcs D F
c
• …
Traversing a Graph
• How do we search a graph?
– At a particular vertex, where shall we go next?
• There are two standard ways:
1. Breadth first search
– Use a queue, to hold nodes for future processing
2. Depth first search
– Use a stack, to hold nodes for future processing
Breadth First Search
• Explore a graph level by level.
• The general idea behind a BFS is beginning at a
starting node A.
• 1. First we examine the starting node A.
• 2. Then we examine all the neighbors of A.
• 3. Then we examine all neighbors of neighbors of
A.
• And so on…
Breadth First Search (Cont…)
• We need to keep track if the neighbors of a node.
• We need to guarantee that no node is processed
more than once.
• This is accomplished by using a queue to hold
nodes that are waiting to be procesed.
An Example of BFS
A B C D
E F G H
I J K L
M N O P
Data Structure and Algorithm
0 A B C D
E F G H
I J K L
M N O P
Data Structure and Algorithm
0 A B C D
1
1 E F 1 G H
I J K L
M N O P
Data Structure and Algorithm
0 A B C D
1 2
1 E F 1 G H
2 I J K L
M N O P
Data Structure and Algorithm
0 A B C D 3
1 2
1 E F 1 G 3 H
2 I J K L
3 M N 3 O P
Data Structure and Algorithm
0 A B C D 3
1 2
1 E F 1 G 3 H 4
2 I J K 4 L 4
3 M N 3 O P
Data Structure and Algorithm
0 A B C D 3
1 2
1 E F 1 G 3 H 4
2 I J K 4 L 4
5
3 M N 3 O P 5
Data Structure and Algorithm
0 A B C D 3
1 2
1 E F 1 G 3 H 4
2 I J K 4 L 4
5
3 M N 3 O P 5
Data Structure and Algorithm
0 A B C D 3
1 2
1 E F 1 G 3 H 4
2 I J K 4 L 4
5
3 M N 3 O P 5
Data Structure and Algorithm
BFS Example
BFS Example
r s t u
v w x y
0
v w x y
Q: s
Data Structure and Algorithm
BFS Example
r s t u
1 0
1
v w x y
Q: w r
Data Structure and Algorithm
BFS Example
r s t u
1 0 2
1 2
v w x y
Q: r t x
Data Structure and Algorithm
BFS Example
r s t u
1 0 2
2 1 2
v w x y
Q: t x v
Data Structure and Algorithm
BFS Example
r s t u
1 0 2 3
2 1 2
v w x y
Q: x v u
Data Structure and Algorithm
BFS Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: v u y
Data Structure and Algorithm
BFS Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: u y
Data Structure and Algorithm
BFS Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: y
Data Structure and Algorithm
BFS Example
r s t u
1 0 2 3
2 1 2 3
v w x y
Q: Ø
Data Structure and Algorithm
Depth First Search
• Go as far as possible along a single path until reach a
dead end, then backtrack.
• The general idea behind a DFS beginning at a starting
node A.
• First we examine the starting node A.
• Then we examine each node along a path which begins
at A.
• After coming to a dead end that is the end of the path,
we backtrack on path until we continue along another
path and so on…
Depth First Search
• The graph below, started at node a, would be visited in the following
order: a, b, c, g, h, i, e, d, f, j
a c d
b
e f
g h i j
J
F
D
E
C G H I I
B B B B B H
A B
A A A A A A
DFS Example
source
vertex
| |
| | |
d: discover time
Data Structure and Algorithm f: finish time
DFS Example
source
vertex
d f
1 | | |
2 | |
| | |
2 | |
3 | | |
2 | |
3 | 4 | |
2 | |
3 | 4 5 | |
2 | |
3 | 4 5 | 6 |
2 | 7 |
3 | 4 5 | 6 |
2 | 7 |
3 | 4 5 | 6 |
2 | 7 9 |
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 |
2 | 7 9 |10
3 | 4 5 | 6 14|
2 | 7 9 |10
3 | 4 5 | 6 14|15
2 | 7 9 |10
3 | 4 5 | 6 14|15
City A
City C
City D
City H
Shortest Paths 66