Directed edge: An edge which has direction is called as directed edge. It is unidirectional.
Undirected edge: An edge which has no specific direction is called as undirected edge.
Directed Graph: It is a graph in which all the edges are directed edges (Di-Graph)
1
Undirected Graph: It is a graph in which all the edges are undirected edges.
1
Mixed Graph: It is a graph in which contain both directed and undirected edges.
1
Isolated node: If a node doesnt have any adjacent node or if a node is not connected by any
edge, that node is said to be isolated node.
1
Loop: An edge in a graph which starts and ends at the same node is called a loop. It can be
either directed or undirected edge.
A
Weighted or labeled Graph: It is a graph that has a number or weight associated with each
edge.
B
D
3
A
B
D
C
E
Sink node: If outdegree of a node is '0' then it is called the sink node.
Representation of Graph:
There are two ways for representing a graph in computer memory.
1. Sequential representation
2. Linked list representation.
1. Sequential representation
Adjacency matrix
It is the matrix, which keeps the information of adjacent nodes.
A[i][j]=1,if there is an edge from node i to node j
=0,if there is no edge from node i to node j
Let us take a graphX
W
Z
z
Adjacency matrix
Similarly we find the adjacency matrix for undirected and weighted graphs.
Refer to class notes for examples.
Linked Representation of Graphs:
It uses linked lists (pointers) and adjacency list.
B D
C A
D A
Step 2: Using Adjacency list, we maintain two kinds of lists in linked representation.
1. A node list
2. Adjacency list
Node list:- It consists of 3 fields
a) Node b) Next c) Adjacent
Node: Value of the node in the graph
Next: Pointer that points to next node in the node list.
Adjacent: points to the first node of the adjacency list of the node.
Node
Next
Adjacency List: It consists of 2 fields.
Adjacent
a) terminal b)pointer
a) Terminal: Contains pointer that points to the node in the node list which is the terminal
node of the edge in the graph
b) Pointer: Contains a pointer that points to the next node in the adjacency list.
Terminal
Pointer
NULL
NULL
NULL
NULL
Insertion of node in adjacency list requires only insertion of that node in header nodes of
adjacency list.
Edge insertionInsertion of an edge requires add operation on the list of the starting node of edge.
Deletion in adjacency listNode deletionDeletion of node requires deletion of that particular node form header node and from the
entire list wherever it is coming. Deletion of node from header will automatically free the list
attached to that node.
Edge deletionDeletion of edge requires deletion in the list of that node where edge starts, and that element
of the list will be deleted where the edge ends.
Refer to class notes for examples or refer to Data structures through C in depth by S. K.
Srivatsava and Deepali Srivatsava
Path Matrix:
Let us take a graph G with n nodes v1, v2, vn. The path matrix or reachable matrix of
G can be defined asP[i][j]=1 if there is a path in between vi and vj
=0
otherwise
If there is a path from vi and vj then it can be a simple path from vi and vj of length n-1 or
less or there can be a cycle of length n or less.
A graph G will be strongly connected if there are no zero entries in path matrix means a path
exists between all vi to vj and vj to vi also.
Computing path matrix form powers of adjacency matrixRefer to class notes.
Warshall's Algorithm to find the path matrix:
We find the path matrix P of a given graph G with the use of powers of adjacency matrix. But
this method is not efficient one. Warshall has given one efficient technique for finding path
matrix of a graph which is called Warshalls algorithm.
A directed graph G with M nodes is maintained in memory by its adjacency matrix A. This
algorithm finds the path matrix P of the graph G.
1. begin
2. Repeat for i,j=1,2,M
Initialises P
Updates P
W
Z
z
=0
otherwise
2
1
7
5
7
Z
4
Algorithm:
A Weighted graph G with M nodes is maintained in memory by its weight matrix W. This
algorithm finds a matrix Q such that Q[i,j] is the length of a shortest path from node VI to VJ.
Infinity is very large number and minimum is the minimum value function.
1. begin
2. Repeat for i,j=1,2,.M [Initialize Q]
If W[i,j]=0 then set Q[i,j]=
Else Q[i,j]= W[i,j]
3. Repeat steps 3 and 4 for k=1,2,..M
[Updates Q]
DFS is an uninformed search that progresses by expanding the first child node of the
search graph that appears and thus going deeper and deeper until a goal node is found,
or until it hits a node that has no children. Then the search backtracks, returning to the
most recent node it hadn't finished exploring. In a non-recursive implementation, all
freshly expanded nodes are added to a LIFO stack for exploration.
Example:
V1
V3
V2
V4
V5
V6
V8
V7
1. Begin at node V1and push V1 into stack. Change its stack to waiting state stack.
V1
2. Pop V1, print V1 and push all the adjacent of V1 into stack, which are still in the ready
state.
V3
V2
V1
3. Pop V3, print V3 and push all the adjacent of V3 into the stack which are still in the ready
state.
V7
V6
V2
V1
V3
4. Pop V7 ,print V7 and push all the adjacent of V7 into stack which are still in the ready state.
V8
V6
V2
V1
V3
V7
5.
V5
V4
V6
V2
V1
V3
V7
6.
V4
V6
V8
V2
V1
V3
V7
V8
V5
Traversing nodes are V1, V3, V7, V8, V5, V4, V6, V2
Algorithm:
1. Start
2. Initialize all the nodes to the ready state.
3. Begin with any node which is in the ready state and push into the stack and change its
status to the waiting state.
4. Repeat the steps 5 and 6 until stack is empty and no nodes are in the ready state.
5. Pop the top node say 'k' of the stack and print it, change its status to the processed state.
6. Push all the adjacent nodes of 'k' which are still in the ready state and change their status to
the waiting state.
7. Stop
Breadth First Search
BFS is an uninformed search method that aims to expand and examine all nodes of
a graph or combinations of sequence by systematically searching through every
solution. In other words, it exhaustively searches the entire graph or sequence without
considering the goal until it finds it.
Algorithm:
1. Start
2. Initialize all the nodes to the ready state.
3. Begin with any node which is in the ready state and insert into the queue, change the status
of the node to the waiting state.
4. Repeat the steps 5 and 6 until queue is empty and no nodes are in the ready state.
5. Remove the front node of queue and print it, change its status to the processed state.
6. Insert all the adjacent nodes of the printed element to the rear of the queue, which are still
in the ready state and change their status to the waiting state.
7. Stop
Example:
V1
V3
V2
V5
V4
1.
V8
V1
2. Print V1
V2
V6
V3
3. Print V2
V3
V4
V5
4. Print V3
V4
V5
V6
V7
V7
6. Print V8
Traverse nodes are V1, V2 , V3,V4, V5 , V6, V7,V8
Traversing Graph:
V1
V2
V4
V3
V6
V5
V7
V8
Sparse Matrix:
If a lot of elements from a matrix have a value 0 then the matrix is known as SPARSE
MATRIX. If the matrix is sparse we must consider an alternate way of representing it rather
the normal row major or column major arrangement. This is because if majority of elements
of the matrix are 0 then an alternative through which we can store only the non-zero elements
and keep intact the functionality of the matrix can save a lot of memory space.
A common way of representing non-zero elements of a sparse matrix is the 3-tuple forms. In
this form each non-zero element is stored in a row, with the 1st and 2nd element of this row
containing the row and column in which the element is present in the original matrix. The 3 rd
element in this row stores the actual value of the non-store element. For example 3-tuple
representation of the sparse matrix as shown in below.
int spmat[10][3]={
7,
7,
9,
0,
3,
-5,
1,
1,
4,
1,
6,
7,
2,
4,
9,
3,
1,
3,
3,
3,
2,
4,
0,
11,
4,
2,
2,
6,
2,