0% found this document useful (0 votes)
2 views

Unit IV College Notes

The document provides an overview of graphs, including their classification into directed and undirected types, and various representation methods such as adjacency matrix, incidence matrix, and adjacency list. It details graph traversal techniques like Depth First Search (DFS) and Breadth First Search (BFS), along with algorithms for Minimum Spanning Trees (MST) like Kruskal's and Prim's, and Dijkstra’s algorithm for finding the shortest path. Additionally, it discusses applications of graphs in social networks, transportation, utilities, document linking, network traffic, and scene representation in graphics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit IV College Notes

The document provides an overview of graphs, including their classification into directed and undirected types, and various representation methods such as adjacency matrix, incidence matrix, and adjacency list. It details graph traversal techniques like Depth First Search (DFS) and Breadth First Search (BFS), along with algorithms for Minimum Spanning Trees (MST) like Kruskal's and Prim's, and Dijkstra’s algorithm for finding the shortest path. Additionally, it discusses applications of graphs in social networks, transportation, utilities, document linking, network traffic, and scene representation in graphics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Chameli Devi Group of Institutions

Department of Computer Science and Engineering


Subject Notes
CS 303- Data Structure
UNIT-IV
Syllabus
Graphs: Introduction, Classification of Graph: Directed and Undirected Graph, Representation, Graph
Traversal: Depth First Search (DFS), Breadth First Search (BFS), Graph Algorithm: Minimum Spanning
Tree (MST)-Kruskal, Prim’s Algorithm. Dijkstra’s Shortest Path Algorithm, Comparison between
Different Graph Algorithms. Application of Graphs.

Graphs:
Graph is a non-linear data structure. It contains a set of points known as nodes (or vertices) and a set
of links known as edges (or arcs). Here edges are used to connect the vertices.
Graph is a collection of nodes and edges in which nodes are connected with edges.
Generally, a graph G is represented as G = (V, E), where V is set of vertices and E is set of edges.
For Example ,Consider a graph with 5 vertices and 6 edges.
Where V = {A,B,C,D,E} and E = {(A,B),(A,C)(A,D),(B,D),(C,D),(B,E),(E,D)}.

Figure 4.1: Graph


Classification of graph:
There are two types of graph:
 Undirected graph: In this graph the edges have no direction.
 Directed graph: In this graph the edges have direction.
Undirected graph:
An undirected graph is a graph whose edges are not directed.
Example

Figure 4.2: Undirected Graph

Directed graph
A directed graph is a graph in which the edges are directed by arrows. Directed graph is also known
as digraph.
Example

Figure 4.3: Directed Graph


In the above graph, each edge is directed by the arrow. A directed edge has an arrow from A to B,
means A is related to B, but B is not related to A.

Representation of Graph
 Adjacency Matrix
 Incidence Matrix
 Adjacency List

Adjacency Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a
total number of vertices.
That means a graph with 4 vertices is represented using a matrix of size 4X4. In this matrix, both rows
and columns represent vertices. This matrix is filled with either 1 or 0. Here, 1 represents that there is
an edge from row vertex to column vertex and 0 represents that there is no edge from row vertex to
column vertex.

Figure 4.4: Adjacency Matrix


Incidence Matrix
In this representation, the graph is represented using a matrix of size total number of vertices by a
total number of edges. That means graph with 4 vertices and 6 edges is represented using a matrix of
size 4X6.
In this matrix, rows represent vertices and a column represents edges. This matrix is filled with 0 or 1
or -1. Here, 0 represents that the row edge is not connected to column vertex, 1 represents that the
row edge is connected as the outgoing edge to column vertex and -1 represents that the row edge is
connected as the incoming edge to column vertex.
Figure 4.5: Incidence Matrix

Adjacency List
In this representation, every vertex of a graph contains list of its adjacent vertices.

Figure 4.6: Adjacency List

Graph Traversal:
 Graph traversal is a technique used for searching vertex in a graph.
 The graph traversal is also used to decide the order in which vertices is visited in the search
process.
 A graph traversal finds the edges to be used in the search process without creating loops.
There are two graph traversal techniques and they are as follows:-
1) DFS (Depth First Search)
DFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without
loops. Create a Stack with maximum size of total number of vertices in the graph to implement DFS
traversal.
Steps to implement DFS traversal:-
Step 1 - Define a Stack of maximum size of total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and
push it on to the stack.
Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of
the stack.
Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from the
stack.
Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges
from the graph
2) BFS (Breadth First Search)
BFS traversal of a graph produces a spanning tree as final result. Spanning Tree is a graph without
loops. Create a Queue with maximum size of total number of vertices in the graph to implement BFS
traversal.
Steps to implement BFS traversal:-
Step 1 - Define a Queue of maximum size of total number of vertices in the graph.
Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and
insert them into the Queue.
Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue
then delete that vertex.
Step 5 - Repeat steps 3 and 4 until queue becomes empty.
Step 6 - When queue becomes empty, then produce final spanning tree by removing unused edges
from the graph

Graph algorithm:
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum possible
number of edges. Hence, a spanning tree does not have cycles and it cannot be disconnected.
Every connected and undirected Graph G has at least one spanning tree. A disconnected graph does
not have any spanning tree, as it cannot be spanned to all its vertices.

Figure 4.7: Graph and Spanning Trees

A complete undirected graph can have maximum nn-2 number of spanning trees, where n is the
number of nodes.

Minimum Spanning Tree (MST)


In a weighted graph, a minimum spanning tree is a spanning tree that has minimum weight than all
other spanning trees of the same graph. In real-world situations, this weight can be measured as
distance, congestion, traffic load or any arbitrary value denoted to the edges.
Kruskal Algorithm:
Kruskal algorithm is used to find the minimum cost spanning tree using the greedy approach. This
algorithm treats the graph as a forest and every node as an individual tree. A tree connects to
another only and only if, it has the least cost among all available options and does not violate MST
properties.
Consider the following example –

Figure 4.8: Example Kruskal Algorithm


Step 1
Remove all self-loops and parallel edges from the
given graph.

Step 2
In case of parallel edges, keep the one which has
the least cost and remove all other parallel edges.

Step 3
Create a set of edges &weight and arrange them in
an ascending order of weight (cost).
Step 4
Add the edge which has the least cost.
The least cost is 2 and edges involved are B,D and
D,T.add these edges in the graph.

Step 5
Next cost is 3, and associated edges are A,C and
C,D. add these edges in the graph.

Step 6
Next cost is 4, and adding these edge will create a
circuit in the graph. So, dont add this edge in the
graph.

Step 7
Next cost is 5 and 6, and adding these edge will
create a circuit in the graph. So, dont add this edge
in the graph.

Step 8
Next cost is 7, and associated edges are S,A. add
these edge in the graph.

Step 9
Next cost is 8, and adding these edge will create a
circuit in the graph. So, dont add this edge in the
graph.

Table 4.1: Numerical Solution Kruskal Algorithm


Prim’s Algorithm:
Prim’s algorithm is used to find minimum cost spanning tree (as Kruskal algorithm) using the greedy
approach.
Prim's algorithm, in contrast with Kruskal algorithm, treats the nodes as a single tree and keeps on
adding new nodes to the spanning tree from the given graph.
Example:-

Figure 4.9: Example Prim’s Algorithm

Step 1
Remove all loops and parallel edges from the given
graph.

Step 2
In case of parallel edges, keep the one which has the
least cost associated and remove all other parallel
edges.

Step 3
Choose S node as the root node of Prim's spanning
tree.
S, A and S, C are two edges with cost 7 and 8,
respectively. Choose the edge S, A as its cost is less
than the other edges.
Step 4
Now, the tree S-7-A is treated as one node and check
for all edges going out from it. Select the one which
has the lowest cost and include it in the graph.

Step 5
After this step, S-7-A-3-C tree is formed. Now, it will
be considered as a node and check all the edges
again. In this case, C-3-D is the new edge, whose cost
is less than other edges cost.

Step 6
After adding node D to the spanning tree, there are
two edges going out of it having the same cost, i.e. D-
2-T and D-2-B. Thus, add both edges.

Table 4.2: Numerical Solution Prim’s Algorithm

Dijkstra’s Shortest Path Algorithm:


 Dijkstra’s Algorithm is a very famous greedy algorithm.
 It is used for solving the single source shortest path problem.
 It computes the shortest path from one particular source node to all other remaining nodes of the
graph.

Steps to implement Dijkstra’s Algorithm:-


Step-01: In the first step two sets are defined-
 One set contains all those vertices which have been included in the shortest path tree.
 In the beginning, this set is empty.
 Other set contains all those vertices which are still left to be included in the shortest path tree.
 In the beginning, this set contains all the vertices of the given graph.
Step-02: For each vertex of the given graph, two variables are defined as-
 Π[v] which denotes the predecessor of vertex ‘v’
 d[v] which denotes the shortest path estimate of vertex ‘v’ from the source vertex.
Initially, the value of these variables is set as-
 The value of variable ‘Π’ for each vertex is set to NIL i.e. Π[v] = NIL
 The value of variable ‘d’ for source vertex is set to 0 i.e. d[S] = 0
 The value of variable ‘d’ for remaining vertices is set to ∞ i.e. d[v] = ∞
Step-03: The following procedure is repeated until all the vertices of the graph are processed-
 Among unprocessed vertices, a vertex with minimum value of variable‘d’ is chosen.
 Its outgoing edges are relaxed.
 After relaxing the edges for that vertex, the sets created in step-01 are updated.

Using Dijkstra’s algorithm, find the shortest distance from source vertex ‘S’ to remaining vertices in
the given graph-

Figure 4.10: Example Dijkstra’s Algorithm

Step-01: The following two sets are created-


Unvisited set: {S, a, b, c, d, e} Visited set: { }
Step-02: The two variables Π and d are created for each vertex and initialized as-
Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e]= NIL
d[S] = 0 d[a] = d[b] = d[c] = d[d] = d[e] = ∞

Step-03 Vertex ‘S’ is chosen. Now, the sets are updated as-
d[S] + 1 = 0 + 1 = 1 < ∞ Unvisited set : {a , b , c , d , e}
d[a] = 1 and Π[a] = S Visited set : {S}
d[S] + 5 = 0 + 5 = 5 < ∞
d[b] = 5 and Π[b] = S

Step-04 Vertex ‘a’ is chosen. Now, the sets are updated as-
d[a] + 2 = 1 + 2 = 3 < ∞ Unvisited set : {b , c , d , e}
d[c] = 3 and Π[c] = a Visited set : {S , a}
d[a] + 1 = 1 + 1 = 2 < ∞
d[d] = 2 and Π[d] = a
d[b] + 2 = 1 + 2 = 3 < 5
d[b] = 3 and Π[b] = a
Step-05 Vertex‘d’ is chosen. Now, the sets are updated as-
d[d] + 2 = 2 + 2 = 4 < ∞ Unvisited set : {b , c , e}
d[e] = 4 and Π[e] = d Visited set : {S , a , d}
Step-06 Vertex ‘b’ is chosen. Now, the sets are updated as-
d[b] + 2 = 3 + 2 = 5 > 2 Unvisited set : {c , e}
No change Visited set : {S , a , d , b}

Step-07 Vertex ‘c’ is chosen. Now, the sets are updated as-
d[c] + 1 = 3 + 1 = 4 = 4 Unvisited set : {e}
No change Visited set : {S , a , d , b , c}

Step-08 Vertex ‘e’ is chosen. Now, the sets are updated as-
Unvisited set : { }
Visited set : {S , a , d , b , c , e}

Table 4.3: Numerical Solution Dijkstra’s Algorithm


Comparison between Different Graph Algorithms
1) If all the edge weights are distinct, then both the algorithms will produce the same Minimum
spanning tree.
Example-

Figure 4.11: Comparsion between Prim’s & Kruskal Algorithm

2) If all the edge weights are not distinct, then both the algorithms may not always produce the same
Minimum spanning tree.
Figure 4.12: Comparsion between Prim’s & Kruskal Algorithm

3) Kruskal Algorithm is preferred when the graph is sparse while Prim’s Algorithm is preferred when
the graph is dense.

Applications of Graphs:
Graphs can be very important in modeling data. In fact, many problems can be reduced to known
graph problems. Applications of graphs are as follows:-
1. Social network graphs: Graphs that represent who knows whom, who communicates with whom,
who influences whom or other relationships in social structures. An example is the twitter graph of
who follows whom. These can be used to determine how information flows, how topics become hot,
how communities develop.
2. Transportation networks: In road networks vertices are intersections and edges are the road
segments between them, and for public transportation networks vertices are stops and edges are the
links between them. Such networks are used by many map programs such as Google maps, Bing
maps and now Apple IOS 6 maps to find the best routes between locations. They are also used for
studying traffic patterns, traffic light timings and many other aspects of transportation.
3. Utility graphs: The power grid, the Internet and the water network are all examples of graphs
where vertices represent connection points and edges represent the wires or pipes between them.
Analyzing properties of these graphs is very important in understanding the reliability of such utilities
under failure or attack, or in minimizing the costs to build infrastructure that matches required
demands.
4. Document link graphs: The best-known example is the link graph of the web, where each web
page is a vertex, and each hyperlink a directed edge. Link graphs are used, for example, to analyze
relevance of web pages, the best sources of information and good link sites.
5. Network packet traffic graphs: Vertices are IP (Internet protocol) addresses and edges are the
packets that flow between them. Such graphs are used for analyzing network security, studying the
spread of worms, and tracking criminal or non-criminal activity.
6. Scene graphs: In graphics and computer games scene graphs represent the logical or spatial
relationships between objects in a scene. Such graphs are very important in the computer games
industry.

You might also like