Unit Vii Graphs Graphs: Comparison Between Graph and Tree
Unit Vii Graphs Graphs: Comparison Between Graph and Tree
Graphs
Graph: - A graph is a collection of two sets V and E. Where V is a finite non empty set of
vertices and E is a finite non-empty set of edges.
A
Vertices are nothing but the nodes in the graph.
Two adjacent vertices are joined by edges B C D
Any graph is denoted as G = {V, E}
For Example: E F
Graph Tree
Non-linear data structure. Non-linear data structure.
Collection of vertices and edges. Collection of vertices and edges.
Each node can have any number of edges. In binary trees every node can have at the most
2 child nodes.
There is no unique node like trees. There is a unique node called root node.
A cycle can be formed. There will not be any cycle.
Applications: used for finding shortest path in Applications: used for expression trees, and
networking graph. decision tree.
Directed graph: - In the directed graph the directions are shown on the edges. As shown in
figure, the edges between the vertices are ordered. In this type of the graph, the edge between
the vertices A and B is set of (A, B). The vertex A is called head and the vertex B is called
tail. We can say edge is set of (A, B) and not of (B, A).
Undirected graph: - In an undirected graph, the edges are not ordered. In the fig, the edge
between A and B is set of (A, B) and (B, A).
A A
DS NEC, CSE Dept. Page 1
B C D B C D
E F E F
UNIT VII GRAPHS
A path is a sequence of vertices in which each vertex is adjacent to the next one. In directed
graph {A, B, C, F} is one path and {A, B, E, F} is another.
Two vertices in a graph are said to be adjacent vertices (or) neighbors if there is a path of length
1 connecting them. In directed graph: B is adjacent to A, where as E is not adjacent to D. In
undirected graph: E and F are adjacent, but D and F are not.
A cycle is a path consisting of at least three vertices that starts and ends with the same vertex.
A loop is a special case of a cycle in which a single arc begins and ends with the same vertex.
A B
A directed graph is strongly connected if there is a path from each vertex to every other vertex
in the digraph.
A directed graph is weakly connected if at least two vertices are not connected.
C D H I
UNIT VII GRAPHS
B E F G
B E F G
The degree of a vertex is the number of lines incident to it. In fig (b), the degree of vertex B is 3
and the degree of vertex E is 2. The out degree of a vertex in a digraph is the number of arcs
leaving the vertex. The in degree of a vertex is the number of arcs entering the vertex. In fig (d)
for the vertex B the in degree is 1 and the out degree is 2.
Adjacency Matrix Representation: Matrix representation is the most commonly useful way
of representing any graph. This representation uses a square matrix of order nXn, where n is the
number of vertices in the graph.
DS NEC, CSE Dept. Page 3
UNIT VII GRAPHS
The matrix is also known as adjacency matrix, because an entry shows the information whether
the two vertices are adjacent (or) not. It is also termed as bit matrix (or) boolean matrix as its
entries are either 1 (or) 0.
V1
V V B E
2 3
V V V V C D
4 5 6 7
1234567 ABCD
1 0110000 A0 0 3 0
2 1001100 B5 0 1 7
3 1000011 C2 0 0 4
4 0100000 D0 6 8 0
6 0010000
The disadvantage of adjacency matrix representation is that it always requires a nXn matrix with
n vertices, regardless of the number of edges. The adjacency matrix for an undirected graph is
symmetric. The adjacency matrix for a directed graph need not be symmetric.
V1 V2 V3 \0
V327654 V321 \0 V64 V75 \0
UNIT VII GRAPHS
\0
Fig (i): Representation of Graph G1.
This is purely the adjacency list graph. The down pointer helps us to go to each next node in the
graph whereas the next node is for going to adjacent node of each of the head node. All the
advantages of linked list can be obtained in this type of graph.
Graph Algorithms: - Data structure is used for storing the graph. The most flexible
structure is the adjacency list implemented as a singly linked list.
outDegree
End GraphVertex
GraphHead structure stores information about each vertex. Graph data is shown in GraphVertex
structure. GraphArc structure stores data about an arc i.e. path between two vertices.
Create Graph: This algorithm initializes the metadata elements for graph data structure:
Algorithm: CreateGraph
1: set count to 0
2: set first to NULL
3: return GraphHead
End CreateGraph
Insert Vertex: Insert vertex adds a disjoint, i.e. an unconnected, vertex to the graph. The arcs
(path) associated with the vertex must be inserted separately.
Algorithm: InsertVertex (graph, dataIn)
1: allocate memory for new vertex
2: store dataIn in new vertex
3: initialize necessary elements in new Node
4: increment graphCount
Now find insertion point
5: if (empty graph)
1: set graph first to new Node
6: else
1: search for insertion point
2: if (inserting before first vertex)
1: set graph first to new vertex
3: else
1: insert new vertex in sequence
7: end if
End InsertVertex
The algorithm is same as singly linked list insertion situation. After allocating memory for the
new vertex, we move in the data and set all of its necessary data values to NULL.
Delete Vertex: In this algorithm, the first thing to do is we need to search for the vertex to be
deleted is present in the graph or not. Once we have found it, we need to ensure that there are no
arcs leaving (or) entering the vertex. If there is an arc, we reject the deletion.
This is basic singly linked list delete situation. The only complexity is that we cannot delete a
vertex if its degree is greater than 0.
Insert Arc: Once we have a vertex, we can connect it to other vertices. Insert Arc requires two
points in the graph: the source vertex and the destination vertex. Each vertex is identified by its
key value rather than by its physical address.
After allocating memory for the new arc, we locate the fromVertex and toVertex. This logic
involves simple linked list searches. If either search fails, we return the appropriate value, -2 for
fromVertex not found and -3 for toVertex not found. If we locate both vertices, we increase there
degree count and set the new arcs destination pointer to the destination vertex. Then, we can
insert the new arc into the adjacency list.
Delete Arc: The delete arc algorithm removes one arc from the adjacency list. To identify an arc,
we need two vertices. The vertices are identified by their key. The algorithm therefore first
searches the vertex list for the start vertex and then searches its adjacency list for the destination
vertex. After locating and deleting the arc, the degree in the from and to vertices is adjusted.
The algorithm processes as follows:
1. Locate the sourceVertex
2. Locate the toVertex
3. Delete the arc
The source vertex is located by searching the vertex list. Once we have located it, we search the
adjacency list for an arc that points to the destination vertex. Once we find the correct arc, we
adjust the degree fields in the from and to vertex entries and then delete the arc.
Algorithm: DeleteArc (graph, fromKey, toKey)
1: if (empty)
1: return -2
2: end if
DS NEC, CSE Dept. Page 8
UNIT VII GRAPHS
Retrieve Vertex: Retrieve vertex return the data stored in a vertex. Given the key of the vertex,
then the date are placed in the output. This algorithm is a typical linked list search algorithm. If
the search is successful, the data in the vertex are placed in the output area specified in the
calling sequence and +1 is returned. If the list is NULL (or) if the data cannot be located, then
return -2.
Algorithm: RetrieveVertex (graph, key, dataOut)
1: if (empty graph)
1: return -2
2: end if
3: search for vertex
4: if (vertex found)
1: move location pointer to dataOut
2: return +1
5: else
1: return -2
6: end if
End RetrieveVertex
Spanning Tree: A spanning tree has a property that for any pair of vertices there exists
only one path between them, and the insertion of any edge to a spanning tree does not form a
F
UNIT VII GRAPHS
cycle. The spanning tree resulting from a call to depth first tree is known as depth first spanning
tree. The spanning tree resulting from a call to breadth first tree is known as a breadth first
spanning tree. The minimum spanning tree for a graph is the spanning tree of minimum cost for
that graph.
Algorithm:
1. Insert the first vertex into the tree.
2. From every vertex already in the tree, examine the total path length to all adjacent vertices
not in the tree. Select the edge with the minimum total path weight and insert it into the tree.
3. Repeat step 2 until all vertices are in the tree.
Prims algorithm:
In the prims algorithm, minimum spanning tree grows in successive stages. At each stage in the
algorithm, we see that whether any of vertices have already been included in the tree, prims
algorithm then finds a new vertex to add it to the tree by choosing the edge <V i, Vj>, the smallest
among all edges, where Vi is the tree and Vj is yet to be included in the tree. The algorithm starts
by selecting a vertex arbitrarily, and then at each stage, we add an edge to the tree.
This algorithm can easily be implemented using adjacency matrix representation of a graph.
Suppose there is a nXn adjacency matrix for a given undirected weighted graph and the vertices
are labeled as V1, V2, V3, .., Vi, Vj, and Vn. Start from Vertex V1. Get its nearest neighbor, which is
the vertex that has the smallest entry in the row of V 1, Let it be Vj. Now consider Vj and V1 as one
sub-graph, i.e., a vertex other than V1 and Vj that has the smallest entry among all the entries in
the rows of V1 and Vj, let his new vertex be V. Therefore, the tree now grows with vertex V 1, Vj,
and V as one sub-graph. This process is repeated until all N vertices have been connected by n-1
edges.
Consider the following example to find minimum spanning tree: An undirected weighted graph
and its weighted adjacency matrix is shown in following figure:
V1 1 2 3 4 5 6 7
1 - 4 5 2 - - -
V2 V3
2 4 - - 1 2 - -
3 5 - - 8 - 1 -
V4 4 2 1 8 - 3 4 7
5 - 2 - 3 - - 9
V5 V6
6 - - 1 4 - - 6
V7 7 - - - 7 9 6 -
Consider vertex V1 and pick the smallest entry in row 1 which is 2 in column 4; this V 4 is the
nearest neighbor to V1, the closest neighbor of sub-graph V 1 and V4 is V2 that can be seen by
examining all entries in rows 1 and 4. The remaining four edges relate to the above procedure.
V1 V1 V1 V1
Initial Tree V2 V2
V4 V4 V4
stage 1 stage 2 stage 3
V5
V1 V1 V1
V2 V2 V3 V2 V3
DS NEC, CSE Dept. Page 11
V4 V4 V4
V5 V5 V6 V5 V6
UNIT VII GRAPHS
V6
V7
Stage 4 stage 5 Final stage
Applications of Graph: -
In computer networking such as Local Area Network (LAN), Wide Area Network
(WAN), internetworking.
In telephone cabling graph theory is efficiently used.
In job scheduling algorithms.
Since graph is nothing but the collection of nodes (vertices) and edges. One can find the best
suited distance between the two vertices is reduced then the cost of calling and the time traveling
through the nodes will get reduced.
Transitive Closure: The Transitive closure matrix, denoted as A+, of a directed graph G, is
a matrix such that
Graph
0 1 2 3 4
0 1 2 3 4
0. 0 1 0 0 0
1. 0 0 1 0 0
2. 0 0 0 1 0 Adjacency Matrix A
3. 0 0 0 0 1
4. 0 0 1 0 0
0 1 2 3 4
1. 0 1 1 1 1
2. 0 0 1 1 1
3. 0 0 1 1 1 Adjacency Matrix A+
4. 0 0 1 1 1
5. 0 0 1 1 1
Dijkstras shortest path: - Dijkstras shortest path is used to find the shortest path
between two vertices in a network. For example, if the network represents the routes flown by an
airline, when we travel we want to find the least expensive route between home and our
destination. When the weights in the route graph are the flight fare, our minimum cost is the
shortest path between 2 nodes. Edsger dijkstra developed a classic algorithm for just this
problem. This algorithm is generally known simply as Dijkstras shortest path algorithm.
V V
2 3
1234
1. 75-- 11 12 - -
2. 7--2 21 - - 24
3. -3-- - 32 - -
4. 4-1- 41 - 43 -
Some of the values of adjacency matrix are infinity (-), which indicates that there is no direct
path between the vertices. The blank sign in right hand side matrix indicates that till now the path
is not determined.
The minimum cost between two vertices is determined by considering one vertex at one time.
Then the path from particular vertex is calculated. If the cost is found to be smaller then previous
cost then it is replaced with the new cost.
Consider the vertex 1 in the given example, the adjacency matrix now holds the values as
follows:
1 2 3 4
1. 7 5 - - 11 12 - -
2. 7 12 - 2 21 212 - 24
3. - 3 - - - 32 - -
4. 4 9 1 - 41 412 43 -
The values shown in square are the values that are added. Here the path 412 is possible as vertex
1 is considered. But the path 124 is not possible because till now the vertex 2 is not considered.
So, via 2 we cannot travel to any other vertex.
Now the vertex 3 is considered, the paths between the vertices 4-2 and 4-4 are changed because
these are the shortest as compared to earlier ones.
1 2 3 4
1. 7 5 - 7 11 12 - 124
2. 7 12 - 2 21 212 - 24
3. 10 3 - 5 321 32 - 324
4. 4 4 1 6 41 432 43 4324
Finally, the vertex 4 is considered, which results into the adjacency matrix that holds the shortest
path between any two vertices.
1 2 3 4
1. 7 5 8 7 11 12 1243 124
2. 6 6 3 2 21 2432 243 24
3. 9 3 6 5 3241 32 3243 324
4. 4 4 1 6 41 432 43 4324
Warshalls Algorithm: -
This algorithm determines if there is a path from any vertex V i to another vertex Vj either directly
(or) through one (or) more intermediate vertices. With this algorithm, we can test the reachability
of all the pairs of vertices in a graph.
To compute the path matrix of a given graph, this algorithm is helpful. This algorithm treats the
entries in the adjacency matrix as bit entries and perform AND (^) and OR (v) boolean
operations on them.
Algorithm:
Input: A graph G whose pointer to its adjacency matrix is GPtr and vertices are labeled as 1, 2,
3, ., N; N being the number of vertices in the graph.
Output: The path matrix P
Data structure: Matrix representation of graph with pointer as GPtr.
Step 1: for i=1 to N do
1: for j=1 to N do
1: P [i][j] = GPtr [i][j]
2: end for
3: for k=1 to N do
1: for i=1 to N do
1: for j=1 to N do
1: P [i][j] = P [i][j] v (P [i][k] ^ P {k][j])
2: end for
2: end for
4: end for
5: return
6: stop
In step1, the path matrix P is initialized with the adjacency matrix GPtr, which signifies the
initial path matrix. This matrix, P [i][j], maintains path which are direct i.e. if there is a direct
path from Vi to Vj.
In step3, the outer most loop will execute N times. For each k, it decides whether there is a path
from Vi to Vj (for all i, j = 1, 2, ., N) either directly (or) via k, i.e., from V i to Vk and then from
Vk to Vj. It therefore sets the P [i][j] entries to 0 (or) 1 accordingly.
Graph Traversals: -
A graph can be traversed in two ways
Depth First Search (DFS)
Breadth First Search (BFS)
1. Both of these algorithms work on directed or undirected graphs.
2. Many advanced graph algorithms are based on the ideas of BFS or DFS.
3. Each of these algorithms traverses edges in the graph, discovering new vertices as it
proceeds.
4. The difference is in the order in which each algorithm discovers the edges.
V1
DS NEC, CSE Dept. Page 16
V V
2 3
V V V8 V
UNIT VII GRAPHS
V21 V2 V3 \0 V
7
V1 V4 V5 \0
(a) Graph G
V3 V1 V6 V7 \0
V4 V2 V8 \0
V5 V2 V8 \0
V6 V3 V8 \0
V7 V3 V8 \0
V8 \0 V4 V5 V6 V7 \0
BFS:
BFS starts at vertex V and mark it as visited. It then visits each of the vertices on Vs adjacency
list. When we have visited all the vertices on Vs adjacency list, we visit all the unvisited vertices
that are adjacent to the first vertex on Vs adjacency list. To implement this scheme, as we visit
each vertex we place the vertex in a queue. When we have exhausted an adjacency list, we
remove a vertex from the queue and proceed by examining each of the vertices on its adjacency
list. Unvisited vertices are visited and then placed on the queue; visited vertices are ignored. The
search is finished when the queue is empty.
V1
Example: Graph G. Source vertex: V1
Visit V1, mark it as visited and Enqueue to Queue. Queue after Enqueue
Dequeue V1 and call BFS on V1.
Find V1 adjacent unvisited node i.e. V2 Queue after Dequeue
Since all adjacent nodes of V2 are visited Dequeue from Queue. Queue after Enqueue
Dequeue V3 and call BFS on V3. V4 V5
Since all adjacent nodes of V3 are visited Dequeue from Queue. Queue after Enqueue
V5 V6 V7
Dequeue V4 and call BFS on V4.
Find V4 adjacent unvisited node i.e. V8. Queue after Dequeue
V5 V6 V7 V8
Visit V8, mark it as visited and Enqueue to Queue.
Since all adjacent nodes of V4 are visited Dequeue from Queue. Queue after Enqueue.
Dequeue V5 and call BFS on V5. V6 V7 V8
Since all adjacent nodes of V5 are visited Dequeue from Queue. Queue after Dequeue
V7 V8
Dequeue V6 and call BFS on V6.
Since all adjacent nodes of V6 are visited Dequeue from Queue. Queue after Dequeue
V8
Dequeue V7 and call BFS on V7.
Since all adjacent nodes of V7 are visited Dequeue from Queue. Queue after Dequeue
If a BFS is initiated from vertex V1, then the vertices of G are visited in the order V 1, V2, V3, V4,
V5, V6, V7, and V8.
DFS:
DFS is similar to a preorder traversal in trees. The DFS begins by visiting the start vertex V. Next
we select an unvisited vertex, W, from Vs adjacency list and carry out a DFS on W. We preserve
our current position in Vs adjacency list by placing it on a stack. Eventually our search reaches a
vertex, U, which has no unvisited vertices on its adjacency list. At this point, we remove a vertex
from the stack and continue processing its adjacency list. Previously visited vertices are
discarded; unvisited vertices are placed on the stack. The traversal terminates when the stack is
empty.
V1
Example: Graph G. Source Vertex V1.
Call DFS on V1. stack
Push V1 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V1) i.e. V2.
Call DFS on V2.
V1 V2
Push V2 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V2) i.e. V4. stack
Call DFS on V4.
Push V4 to stack & output, Mark as visited. V1 V2 V4
Find Adjacent unvisited node of stack top (V4) i.e. V8. stack
Call DFS on V8.
V1 V2 V4 V8
Push V8 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V8) i.e. V5. stack
Call DFS on V5.
V1 V2 V4 V8 V5
Find Adjacent unvisited node of stack top (V3) i.e. V7. stack
Call DFS on V7.
V1 V2 V4 V8 V6 V3 V7
Push V7 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V7) but all nodes are visited. stack
V1 V2 V4 V8 V6 V3
If a DFS is initiated from vertex V1, then the vertices of G are visited in the order V 1, V2, V4, V8,
V5, V6, V3 and V7.