Sarthak Tomar53 Unit-4 DAA
Sarthak Tomar53 Unit-4 DAA
University
Nawabganj , Kanpur
Submitted To : Submitted By :
Dr. Imran Khan Sarthak Tomar
Assistant Professor 200108053
CSE Department Information Technology (III)
Unit-4 (Graph Algorithms)
Graph:
A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also
referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph. More
formally a Graph is composed of a set of vertices( V ) and a set of edges( E ). The graph is denoted by
G(E, V).
Components of Graph:
Vertices: Vertices are the fundamental units of the graph. Sometimes, vertices are also known
as vertex or nodes. Every node/vertex can be labeled or unlabelled.
Edges: Edges are drawn or used to connect two nodes of the graph. It can be ordered pair of
nodes in a directed graph. Edges can connect any two nodes in any possible way. There are no
rules. Sometimes, edges are also known as arcs. Every edge can be labeled/unlabelled.
Representation of Graphs:
Adjacency Matrix:
Adjacency Matrix is a 2D array of size V x V where V is the number of vertices in a graph. Let the 2D
array be adj[][], a slot adj[i][j] = 1 indicates that there is an edge from vertex i to vertex j. Adjacency
matrix for undirected graph is always symmetric. Adjacency Matrix is also used to represent weighted
graphs. If adj[i][j] = w, then there is an edge from vertex i to vertex j with weight w.
Adjacency List:
An array of lists is used. The size of the array is equal to the number of vertices. Let the array be an
array[]. An entry array[i] represents the list of vertices adjacent to the ith vertex. This representation
can also be used to represent a weighted graph. The weights of edges can be represented as lists of
pairs. Following is the adjacency list representation of the above graph.
Graph Traversal Algorithms:
1. Depth First Search (DFS):
The DFS algorithm starts from a starting node u. In each step, the algorithm picks one of the
non-visited nodes among the adjacents of u and performs a recursive call starting from that
node. When all the neighbors of a node are visited, then the algorithm ends for the node u and
returns to check the neighbors of the node that initiated the call to node u.
Algorithm:
DFS(u):
1. If(visited[u] = True):
2. Return
3. End if
4. Print(u)
5. visited[u] = True
6. for node that are connected to u:
7. DFS(node)
8. End for
Complexity:
Time Complexity : O(V+E)
Space Complexity : O(V)
Applications:
1. Detect cycle in graph.
2. Path finding from one node to another.
3. Find connected components
4. Topological sorting
2. Breadth First Search:
BFS is a traversing algorithm where you should start traversing from a selected node (source or
starting node) and traverse the graph layerwise thus exploring the neighbour nodes (nodes which
are directly connected to source node). You must then move towards the next-level neighbour
nodes.
As the name BFS suggests, you are required to traverse the graph breadthwise as follows:
1. First move horizontally and visit all the nodes of the current layer
2. Move to the next layer
Algorithm:
BFS (G, s):
1. let Q be queue.
2. Q.enqueue( s )
3. mark s as visited
4. while (Q is not empty)
5. v = Q.dequeue( )
6. for all neighbors w of v in Graph G
7. if w is not visited
8. Q.enqueue( w )
9. mark w as visited
10. End if
11. End for
12. End while
Complexity:
Time Complexity: O(V+E)
Space Complexity : O(V)
Applications:
1. Find path between nodes.
2. Shortest path between nodes.
3. Cycle detection in undirected graph.
4. GPS navigation system
5. Finding nodes in connected components.
Minimum Spanning Tree (MST):
A spanning tree is a sub-graph of an undirected connected graph, which includes all the vertices of the
graph with a minimum possible number of edges. If a vertex is missed, then it is not a spanning tree.
The cost of the spanning tree is the sum of the weights of all the edges in the tree. There can be many
spanning trees. Minimum spanning tree is the spanning tree where the cost is minimum among all the
spanning trees. There also can be many minimum spanning trees.
Minimum spanning tree has direct application in the design of networks. It is used in algorithms
approximating the travelling salesman problem, multi-terminal minimum cut problem and minimum-
cost weighted perfect matching.
Prim’s Algorithm:
Prim’s Algorithm also use Greedy approach to find the minimum spanning tree. In Prim’s Algorithm
we grow the spanning tree from a starting position. Unlike an edge in Kruskal's, we add vertex to the
growing spanning tree in Prim's.
The time complexity of the Prim’s Algorithm is O((V+E)logV) because each edge is inserted in the
priority queue only once and insertion in priority queue take logarithmic time.
There are following steps in Prim’s algorithm:
Step 1: Select a starting vertex
Step 2: Repeat Steps 3 and 4 until there are fringe vertices
Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weight
Step 4: Add the selected edge and the vertex to the minimum spanning tree T
[END OF LOOP]
Step 5: EXIT
Kruskal’s Algorithm:
Kruskal's Algorithm is used to find the minimum spanning tree for a connected weighted graph. The
main target of the algorithm is to find the subset of edges by using which we can traverse every vertex
of the graph. It follows the greedy approach that finds an optimum solution at every stage instead of
focusing on a global optimum.
In Kruskal’s algorithm, most time consuming operation is sorting because the total complexity of the
Disjoint-Set operations will be O(ElogV), which is the overall Time Complexity of the algorithm.
Algorithm:
FLOYD - WARSHALL (W):
1. n ← rows [W].
2. D0 ← W
3. for k ← 1 to n
4. do for i ← 1 to n
5. do for j ← 1 to n
6. do dij(k) ← min (dij(k-1),dik(k-1)+dkj(k-1) )
7. return D(n)
Example:
Example:
Travelling Salesman Problem:
Travelling salesman problem is the problem to find the shortest possible route for a given set of cities
and distance between the pair of cities that visits every city exactly once and returns to the starting
point. Backtracking approach is used to solve TSP problem.
Let us consider a graph G = (V, E), where V is a set of cities and E is a set of weighted edges. An
edge e(u, v) represents that vertices u and v are connected. Distance between vertex u and v is d(u, v),
which should be non-negative.
Suppose we have started at city 1 and after visiting some cities now we are in city j. Hence, this is a
partial tour. We certainly need to know j, since this will determine which cities are most convenient to
visit next. We also need to know all the cities visited so far, so that we don't repeat any of them. Hence,
this is an appropriate sub-problem.
For a subset of cities S Є {1, 2, 3, ... , n} that includes 1, and j Є S, let C(S, j) be the length of the
shortest path visiting each node in S exactly once, starting at 1 and ending at j.
When |S| > 1, we define C(S, 1) = ∝ since the path cannot start and end at 1.
Algorithm: Traveling-Salesman-Problem
TSP(G,n):
1. C ({1}, 1) = 0
2. for s = 2 to n do
3. for all subsets S Є {1, 2, 3, … , n} of size s and containing 1
4. C (S, 1) = ∞
5. for all j Є S and j ≠ 1
6. C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}
7. Return minj C ({1, 2, 3, …, n}, j) + d(j, i)
Complexity:
There are at the most 2n.n2n.n sub-problems and each one takes linear time to solve. Therefore, the
total running time is O(2n. n2).
.