1
Graph
2) A set E of edges such that each edge e in E is identified with unique pair[u,v] of
nodes in V denoted by e=[u,v]
a) Suppose e=[u,v] then the nodes u and v are called the endpoints of e and u and
v are said to be adjacent nodes or neighbor.
if deg(u)=0 i.e if u does not belong to any edge then u is called an isolated node.
2
Graph
6) A Graph G is said to be connected if there is a path between any two of its nodes.
8) A connected Graph T without any cycles is called a tree graph or free tree.
9)If T is a finite tree with m nodes, then T will have m-1 edges
3
10) A Graph is said to be labeled if its edges are assigned a data.
a) Multiple edges: Distinct edges e and e’ are called multiple edges if they
connect the same end points that is if e=[u,v] and e’=[u,v].
Suppose G is directed Graph with directed edge e=(u,v). Then e is also called an arc. The following terminology is
used.
The outdegree of a NODE u in G is written outdeg(u), is the number of edges beginning at u. similarly the indegree
of u written indeg(u) is the number of edges ending at u.
A node u is called Source if it has a positive outdegree but zero indegree. Similarly, u is called a sink if it has a zero
outdegree but a positive indegree.
5
• A directed graph G is said to be connected, or strongly connected if for each pair u,v
of nodes in G there is a path from u to v and there is also a path from v to u.
0 0 1 0
W
Z
6
Linked Representation of Graph
• The sequential Representation of G in memory i.e by adjacency matrix has a number of
major drawbacks.
• First of all it may be difficult to insert and delete nodes in G. This is because the size of
A may need to be changed and the nodes may need to reordered, so there may be many
many changes in the matrix A.
A
D Nod Adjacency
e List
E A B,C,D
B C
B
C
C
D C,E
E C
Adjacency List
7
NODE NEXT ADJ
Node List: Each element in the list NODE will correspond to a node in G and it will be a record of the form:
• Here node will be the name or key value of the node, Next will be a pointer to the next node in the list NODE and
ADJ will be a pointer to the first element in the adjacency list of the node, which is maintained in the edge list.
• The shaded area indicates that there may be other information in the record, such as the indegree INDEG of the
node, the outdegree of the node, the status of the node during the execution of an algorithm and so on.
Edge List: Each element in the list EDGE will correpsond to an edge and will be a record of the form:
The field DEST will point to the location in the list NODE of the destination or terminal node of the edge. The field
LINK will link together the edges with the same initial node that is the nodes in the same adjacency list.
DEST LINK
8
DEST LINK
NODE NEXT ADJ 1 2 (c ) 7
Start=
4 1 3 5
2 C 9 0 2
3 7 (B) 10
8 4 9 (D) 0
3
4 A 7 3 5 8
5 1
6 2 (C) 0
E 0 11 7 6 (E) 0
6
7 B 2 6 9
8 8
10 12
9
D 6 1 2 (C) 4
9
10 0 10
2 (C) 0
11
12 0
9
Traversing a graph
• Breadth First Search: The BFS will use a queue as an auxiliary structure to hold
nodes for future processing
During the execution of our algorithms, each node N of G will be in one of the three
states called STATUS of N as Follows:
10
BFS
11
Algorithm
1) Initialize all nodes to the ready state( STATUS=1)
2) Put the starting node A in QUEUE and change its status to the waiting
state(STATUS=2)
4) Remove the front node N of queue. Process N and change the status of N to
the processed state( STATUS=3)
5) Add to the rear of queue all the neighbors of N that are in the steady
state( Status =1) and change their status to the waiting state( Status =2)
6) Exit
12
Adjacency List
A A F,C,B
B G,C
F C B C F
D C
E D,C,J
E
F D
D G
G C,E
J D,K
J K K E,G
13
1) Initially, add A to queue and add NULL to ORIG as follows FRONT=1 Rear=1 Queue: A ORIG: 0
2) Remove the front element A from Queue by setting Front=Front+1 and add to Queue neighbors of A as
follows: FRONT=2 REAR=4 Queue=A,F,C,B ORIG: 0,A,A,A . The origin A of each of the three edges
is added to Orig
3) Remove the front element F from Queue by setting Front=Front+1 and add to Queue neighbors of F as
follows FRONT=3 ,REAR=5 Queue: A,F,C,B,D Orig: 0,A,A,A,F
4) Remove the front element C from Queue by setting Front=Front+1 and add to Queue neighbors of C as
follows FRONT=4 ,REAR=5 Queue: A,F,C,B,D Orig: 0,A,A,A,F
Note The neighbor F of C is not added to QUEUE Since F is not in the ready state( Because F has already
been added to queue)
5) Remove the front element B from Queue by setting Front=Front+1 and add to Queue neighbors of B as
follows FRONT=5 ,REAR=6 Queue: A,F,C,B,D,G Orig: 0,A,A,A,F,B
Note that only G is added to queue since the other neighbor C is not in the ready state.
14
• Remove the front element D from Queue by setting Front=Front+1 and add to Queue neighbors of
D as follows FRONT=6 ,REAR=6 Queue: A,F,C,B,D,G Orig: 0,A,A,A,F,B
• Remove the front element G from Queue by setting Front=Front+1 and add to Queue neighbors of
G as follows FRONT=7 ,REAR=7 Queue: A,F,C,B,D,G,E Orig: 0,A,A,A,F,B,G
• Remove the front element E from Queue by setting Front=Front+1 and add to Queue neighbors of
E as follows FRONT=8 ,REAR=8 Queue: A,F,C,B,D,G,E,J Orig: 0,A,A,A,F,B,G,E
• We stop as soon as J is added to Queue, since J was final destination. Now we backtrack from J
using the array Orig to find path P.
• Thus, J E G B A
15
DFS
1) Initialize all nodes to the ready state (Status=1)
2) Push the starting node A on to Stack and change its status to the waiting
state(Status=2)
4) Pop the top node N of Stack Process N and Change its Status to the Processed
state( Status=3)
5) Push on to stack all the neighbors of N that are still in the ready state(status=1)
and change their status to the waiting state( status=2)
6 Exit
16
DFS
(a) Initially, push J onto stack
Stack: J
(b) Pop and print the top element J, and push onto stack all the neighbours of J which are in ready state
Print J Stack: D, K
(c) Pop and print the top element K, and push onto stack all the neighbours of K which are in ready
state
Print K Stack: D, E, G
(d) Pop and print the top element G, and push onto stack all the neighbours of G which are in ready
state
Print G Stack: D, E, C
17
DFS
(e) Pop and print the top element C, and push onto stack all the neighbours of C which are in ready state
Print C Stack: D, E, F
(f) Pop and print the top element F, and push onto stack all the neighbours of F which are in ready state
Print F Stack: D, E
(g) Pop and print the top element E, and push onto stack all the neighbours of E which are in ready state
Print E Stack: D
(h) Pop and print the top element D, and push onto stack all the neighbours of D which are in ready state
18
Warshalls Algorithm
19
20
21
22
23
24
25