Anda di halaman 1dari 18

GRAPHS

Graph is a set of nodes and edges.


G=[V,E]
V represent nodes or vertices
V={v1, v2,.. vn}
E represent edges or pair of nodes
i.e., E=[( vi, vj)]
Example: Train route which connects cities. Cities are nodes and routes are edges.
1

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

Path: A Path in a graph is a sequence of connected nodes.

A
B
D

C
E

Path is from A to E. i.e., A,B,C,E.


Number of edges in the path gives length of the path. i.e., the length from A to E is 3.
Complete Graph: If there is an edge between every pair of nodes then it is a complete graph.
In undirected graph, number of edges with n number of nodes is n*(n-1)/2 edges.
In directed graph, number of edges with n number of nodes is n*(n-1) edges.

Degree of a node: Number of edges connected to that node.

Indegree: Number of edges terminated at that node.


Out degree: Number of edges starting at that node.
Indegree of node A =2
Outdegree of node A =0
Indegree of node B =1
Outdegree of node B =1
Total degree: The sum of indegree and outdegree is called total degree.
Total degree of A is 2
Source node: If indegree of a node is '0' then it is called the source node.

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

The corresponding adjacency matrix for this graph is-

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.

Step !: 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

Operations on graphGraph can be represented in two ways


1. Sequential representation
2. Linked list representation.
The two main operations on graph are1. Insertion
2. Deletion
Here insertion and deletion will also be on two things1. On node
2. On edge
Now we have insertions and deletion operation on adjacency matrix and adjacency list.
Insertion in Adjacency matrix
Node insertionInsertion of node requires only addition of one row and one column with zero entries in
that row and column.
Refer to class notes for examples
Edge insertionThe entry of adjacency matrix 1 represents the edge between two nodes, and 0 represents
no edge between those two nodes. Therefore insertion of edge requires changing the value
0 into 1 for those particular nodes.
Deletion in adjacency matrixNode deletionDeletion of node requires deletion of that particular row and column in adjacency matrix
for node to be deleted, because deletion requires deletion of all the edges which are
connected to that particular node.
Edge deletionDeletion of an edge requires changing the value 1 to 0 for those particular nodes.
Insertion in adjacency listNode Insertion-

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

If A[i,j]=0 then set p[i,j]=0


Else p[i,j]=1
End the loop
3. Repeat step 3 and 4 for k=1,2,3,M

Updates P

4. Repeat step 4 for i=1,2,3,M


5. Repeat for j=1,2,M
Set Pk[i,j]= Pk-1[i,j]{ Pk-1[i,k] Pk-1[k,j]}
End loop
End step 3 loop
End step 2 loop
6. Exit
Example:

W
Z
z

Pk[i,j]= Pk-1[i,j]{ Pk-1[i,k] Pk-1[k,j]}

Shortest Path Algorithm/Modified Warshall's Algorithm:


Warshalls algorithm gives the path matrix of graph. Now by modifying this algorithm, we
find the shortest path matrix Q. Here Q[i][j] will represent the length of shortest from vi to vj.
There can be many paths from any node vi to vj.
W[i][j]=weight on edge,

if there is an edge from node I to node j.

=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]

4. Repeat step 4 for i=1,2,..M


5. Repeat step 5 for j=1,2,M
Set Qk[i,j]=Min[Qk-1[i,j], Qk-1[i,k]+ Qk-1[k,j]]
6. Exit

Graph Traversal Methods:


Traversal in graph is different from traversal in tree or list because of the following reasons:
a. There is no first node or root node in a graph, hence the traversal can start from any
node.
b. In tree or list when we start traversing from the first node, all the nodes are traversed
but in graph only those nodes will be traversed which are reachable from the starting
node. So if we want to traverse all the reachable nodes we again have to select another
starting node for traversing the remaining nodes.
c. In tree or list while traversing we never encounter a node more than once but while
traversing a graph, there may be a possibility that we reach a node more than once. So
to ensure that each node is visited only once we have a kept the status of each node
whether it has been visited or not.
d. In tree or list we have unique traversals. For example if we are traversing the tree in
inorder there can be only one sequence in which nodes are visited. But in graph, for
the same technique of traversal there can be different sequences in which nodes can
be visited.
DFS Traversal or Depth first search
BFS Traversal or Breadth first search
DFS: It uses DS stack. The elements are traversed in depth wise.

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

5. Print V4, V5 , V6, V7


V8

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,

Anda mungkin juga menyukai