0% found this document useful (0 votes)
73 views9 pages

Sarthak Tomar53 Unit-4 DAA

The document summarizes various graph algorithms including: - Depth First Search (DFS) and Breadth First Search (BFS) for graph traversal. - Minimum Spanning Tree algorithms like Prim's and Kruskal's which find minimum cost spanning trees. - Single source shortest path algorithms like Dijkstra's and Floyd-Warshall for finding shortest paths between vertices. - The Travelling Salesman Problem (TSP) which finds the shortest route visiting each city once and returning to the start. Backtracking is used to solve TSP.
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)
73 views9 pages

Sarthak Tomar53 Unit-4 DAA

The document summarizes various graph algorithms including: - Depth First Search (DFS) and Breadth First Search (BFS) for graph traversal. - Minimum Spanning Tree algorithms like Prim's and Kruskal's which find minimum cost spanning trees. - Single source shortest path algorithms like Dijkstra's and Floyd-Warshall for finding shortest paths between vertices. - The Travelling Salesman Problem (TSP) which finds the shortest route visiting each city once and returning to the start. Backtracking is used to solve TSP.
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/ 9

Harcourt Butler Technical

University
Nawabganj , Kanpur

Design & Analysis of Algorithm


(ECS-355)

Session : 2022 – 2023

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.

There are following steps in Kruskal’s algorithm:


o First, sort all the edges from low weight to high.
o Now, take the edge with the lowest weight and add it to the spanning tree. If the edge to be
added creates a cycle, then reject the edge.
o Continue to add the edges until we reach all vertices, and a minimum spanning tree is created.
Floyd Warshall Algorithm:
Floyd-Warshall Algorithm is an algorithm for finding the shortest path between all the pairs of vertices
in a weighted graph. This algorithm works for both the directed and undirected weighted graphs. But, it
does not work for the graphs with negative cycles (where the sum of the edges in a cycle is negative).
This algorithm follows the dynamic programming approach to find the shortest paths.
Time complexity of the Floyd-Warshall algorithm is O(n3).
The space complexity of the Floyd-Warshall algorithm is O(n2).

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:

A4 gives the shortest path between each pair of vertices.


Dijkstra’s Algorithm:
Dijkstra's algorithm allows us to find the shortest path between any two vertices of a graph.
It differs from the minimum spanning tree because the shortest distance between two vertices might not
include all the vertices of the graph.
The algorithm uses a greedy approach in the sense that we find the next best solution hoping that the
end result is the best solution for the whole problem.
Algorithm:
1 function Dijkstra(Graph, source):
2 dist[source] ← 0 // Initialization
3 create vertex priority queue Q
6 for each vertex v in Graph.Vertices:
4 if v ≠ source
5 dist[v] ← INFINITY // Unknown distance from source to v
6 prev[v] ← UNDEFINED // Predecessor of v
7 Q.add_with_priority(v, dist[v])
8 while Q is not empty: // The main loop
9 u ← Q.extract_min() // Remove and return best vertex
10 for each neighbor v of u: // Go through all v neighbors of u
11 alt ← dist[u] + Graph.Edges(u, v)
12 if alt < dist[v]:
13 dist[v] ← alt
14 prev[v] ← u
15 Q.decrease_priority(v, alt)
16 return dist, prev

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).
.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy