Professional Documents
Culture Documents
Lecture3434_16870_Graphs 1
Lecture3434_16870_Graphs 1
c E= {(a,b),
(a,c),(a,d),
(b,e),(c,d),
d e (c,e),
(d,e)}
A graph consists G of
G=(V,E)
If e = [ u , v ] then
end points of e : u , v
Adjacent nodes or neighbor nodes : u , v
degree of a node : number of edges in node
isolated node : if deg(u) = 0
Complete graph : If every node u in G is adjacent to every other node v in G. A complete graph
will have n (n-1) /2 edges
Weights : non negative numeric value, may be Length of the edge. If no weight is specified then
w(e) = 1 is considered
Different terminology used for end points : Begin End, Origin and terminal point, predecessor and
successor
Strongly connected directed graph : if for each pair of node there is a path from u to v and v to u
Unilaterally connected directed graph : if for each pair of node there is a path from u to v or v to u
Applications
CS16
electronic circuits
JFK
LAX STL
HNL
DFW
FTL
Examples: 0
3
0 2
1 2
3 1 2 3 3 3
3 4 5 6
3
G1 3 1 1 1 1
G2
directed graph
0 1 in: 1, out: 2
in-degree
in:1, out: 1 out-degree
2 in: 1, out: 0
G3
Terminology:
Path
path: sequence of
3 2
vertices v1,v2,. . .vk such
that consecutive
vertices vi and vi+1 are 3
adjacent.
3 3
a b a b
c c
d e d e
abedc bedc
7
More Terminology
simple path: no repeated vertices
a b
bec
c
d e
cycle: simple path, except that the last vertex is the same as
the first vertex
a b
acda
c
d e
Even More Terminology
1 1 1 1
2 2 2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
G3
More…
tree
tree
forest
tree
tree
Connectivity
n 5
m (5
More Connectivity
n = #vertices
n5
m = #edges m4
For a tree m = n - 1
If m < n -
1, G is not
connected
n5
m3
Oriented (Directed) Graph
A graph where edges are directed
Directed vs. Undirected Graph
An undirected graph is one in which the pair
of vertices in a edge is unordered, (v0, v1) =
(v1,v0)
A directed graph is one in which each edge is
a directed pair of vertices, <v0, v1> != <v1,v0>
tail head
Adjacency Matrix
1 2
1
3
0 1 2 3 2
1 0 2 3
0 1
2 0 1 3
1 0 2
3 0 1 2
2
G1 G2
Sequential Representation of Graphs : Adjacency Matrix
and Path Matrix
Y X
Z W
0 0 0 1 0 0 1 0 1 0 0 1 0 0 1 1
1 0 1 1 1 0 1 2
A1 = A2 = A3 = 1 0 2 2 2 0 2 3
1 0 0 1 0 0 1 1 1 0 1 1 A4 = 1 0 1 2
0 0 1 0 1 0 0 1 0 0 1 1 1 0 1 1
• entry of matrix Br gives the number of paths of length r or less from node v i to vj
Path matrix: Let G be simple directed graph with m
nodes (v1, v2, ....... vm ). Then, path matrix or reach-
ability matrix of G is:
Pij = 1 (if there is path from vi to vj)
= 0 (otherwise)
B = A1 + A2 + A3 + A4
1 0 1 1
Warshall’s Algorithm
It is used to find path matrix in efficient way,
rather than calculating powers of adjacency
matrix.
= 0 (otherwise)
Warshall’s Algorithm
Warshall observed that Pk [i, j] = 1 can occur in
following 2 cases:
• Accordingly:
Pk [i, j] = Pk-1 [i, j] v (Pk-1 [i, k] ^ Pk-1 [k, j] )
Finding transitive closure
3
3 1
1
2 4
4
2 3 R2
R1 1 0 0 1 0
R0
0 0 1 0 0 0 1 0 1 0 1 1
1 0 0 1 1 0 1 1 0 0 0 0
4
0 0 0 0 0 0 0 0 2 1 1 1 1
0 1 0 0 0 1 0 0
3 R4
3 R 3
1
1 0 0 1 0 0 0 1 0
1 0 1 1 1 1 1 1
0 0 0 0 4 0 0 0 0
2
4 1 1 1 1 1 1 1 1
2
Warshall’s Algorithm
A directed graph G with M nodes is maintained in memory by its
adjacency matrix A. This algo. finds path matrix P of graph G.
7 5 ∞ ∞ 7 5 ∞ ∞
7 ∞ ∞ 2 7 12 ∞ 2
Q0 = Q1 =
∞ 3 ∞ ∞ ∞ 3 ∞ ∞
4 ∞ 1 ∞ 4 9 1 ∞
7 5 ∞ 7
7 5 8 7
7 5 ∞ 7 7 12 ∞ 2
Q3 =
6 6 3 2
7 12 ∞ 2 10 3 ∞ 5 Q4 =
Q2 =
9 3 6 5
10 3 ∞ 5 4 4 1 6
4 4 1 6
4 9 1 11
Shortest Path (Modified Warshall’s algorithm)
• Accordingly:
Qk [i, j] = Min (Qk-1 [i, j] , Qk-1 [i, k] + Qk-1 [k, j] )
Shortest Path (Modified Warshall’s algorithm)
• Two methods
• BFS will use Queue and DFS will use Stack as auxiliary storage
•The algorithm will process only those nodes which are reachable from the starting node
•During the execution of the algorithm, each node N of G will be in one of the three states, called
status of N
2.Put the starting node A in Queue and change its status to waiting, STATUS = 2
5. Add to the rear of Queue all neighbors of N that are in ready state (STATUS
= 1) and change their STATUS to waiting state ( STATUS = 2 )
6.Exit
Node Adj
List
A F, C , B
B G, C
C F
D C
E D, C, J
F D
G C, E
J D, K
K E, G STEP QUEUE ORIGIN F R
1 A 0 1 1
2 AFCB 0AAA 2 4
3 AFCBD 0AAAF 3 5
4 AFCBD 0AAAF 4 5
5 AFCBDG 0AAAFB 5 6
6 AFCBDG 0AAAFB 6 6
7 AFCBDGE 0AAAFBG 7 7
8 AFCBDGEJ 0AAAFBGJ 8 8
9 … … .. ..
10 … …. .. ..
Applications of Breadth First Search
2.Push the starting node A in STACK and change its status to waiting,
STATUS = 2
5. Push onto STACK all neighbors of N that are in ready state (STATUS
= 1) and change their STATUS to waiting state ( STATUS = 2 )
6.Exit
Iterative
Recursive
Node Adj List
A F, C , B
B G, C
C F
D C
E D, C, J
F D
G C, E
J D, K
K E, G
1 A A
2 FCB AB
3 FCG ABG
4 FCE ABGE
5 FCDJ ABGEJ
6 … …
Applications of Depth First Search
B C
B C
D C, E
E C
Sequential representation of graph G in
memory using adjacency matrix A has some
drawbacks.
It is difficult to insert/delete nodes in G (since size
of A needs to be changed & nodes to be
reordered).
Matrix A may be sparse (containing many zeros
for no edges) in which great deal of memory is
wasted.
Operation on Graph
Searching:
Find(info, link, start, item, loc)
Finds the location loc of first node containing item, or sets loc
= NULL
1. [List empty?] If start = NULL, then Set flag = False and return
2. [item in first node?] If info[start] = item, then
Set Ptr = start, start = link[start]
link[Ptr] = avail, avail = Ptr
flag = true and return
3. Set Ptr = link[start] and save = start [initializes pointers]
4. Repeat steps 5 and 6 while Ptr != NULL
5. If Info[Ptr] = item, then
Set link[save] = link[Ptr], link[Ptr] = avail
avail = Ptr, flag = True and return
Set save = Ptr and Ptr = link[Ptr] [updates pointers]
6. Set flag = False and return
Inserting:
InsNode(node, next, Adj, start, availN, N, flag)
This procedure inserts node N in graph G.