Graphs in ds2 Bca 4
Graphs in ds2 Bca 4
10. Cyclic Graph: A graph containing at least one cycle is known as a Cyclic
graph.
11. Directed Acyclic Graph: A Directed Graph that does not contain any cycle.
12. Bipartite Graph: A graph in which vertex can be divided into two sets such
that vertex in each set does not contain any edge between them.
13. Weighted Graph: A graph in which the edges are already specified with
suitable weight is known as a weighted graph. Weighted graphs can be further
classified as directed weighted graphs and undirected weighted graphs.
Tree v/s Graph: Trees are the restricted types of graphs. Every tree will
always be acyclic graph but not all graphs will be trees. Linked List, Trees,
and Heaps all are special cases of graphs.
Representation of Graphs : There are two ways to store a graph: Adjacency
Matrix & Adjacency List
Adjacency Matrix: In this method, the graph is stored in the form of the 2D
matrix where rows and columns denote vertices. Each entry in the matrix
represents the weight of the edge between those vertices.
Adjacency List: This graph is represented as a collection of linked lists.
There is an array of pointer which points to the edges connected to that
vertex.
Basic Operations on Graphs
Below are the basic operations on the graph:
Insertion of Nodes/Edges in the graph – Insert a node into the graph.
Deletion of Nodes/Edges in the graph – Delete a node from the graph.
Searching on Graphs – Search an entity in the graph.
Traversal of Graphs – Traversing all the nodes in the graph.
Binary Operations on Graphs
Let G1 = (V1, E1) and G2 = (V2, E2) then
Union of graphs- the union of two graphs (G1 U G2) is defined as the
graph (V1 ∪ V2, E1 ∪ E2).
Intersection of graphs, G1 ∩ G2 = (V1 ∩ V2, E1 ∩ E2);
Complement of a graph, G is a graph G’ on the same set of vertices as of
G such that there will be an edge between two vertices (v, e) in G’, if
and only if there is no edge in between (v, e) in G. (i.e. complete graph -
G=g’)
Cartesian product of graph. (G □ H) of graphs G and H is a graph:
the vertex set of G □ H is the Cartesian product V(G) × V(H) and two
vertices (u,v) and (u' ,v' ) are adjacent in G □ H if and only if either
u = u' and v is adjacent to v' in H, or v = v' and u is adjacent to u' in G.
Ring sum of graphs, given two graphs G1 = (V1, E1) and G2 = (V2, E2)
we define the ring sum G1 ⊕ G2 = (V1 ∪ V2, (E1 ∪ E2) − (E1 ∩ E2))
with isolated points dropped. So, an edge is in G1 ⊕ G2 if and only if it
is an edge of G! or an edge of G2, but not both.
// consider example of class.
Dijkstra’s Algorithm
Step 2: Check for adjacent Nodes, now we have to choices (Either choose
Node1 with distance 2 or either choose Node 2 with distance 6) and choose
Node with minimum distance. In this step Node 1 is Minimum distance
adjacent Node, so marked it as visited and add up the distance.
Distance: Node 0 -> Node 1 = 2
Dijkstra’s Algorithm
Step 3: Then Move Forward and check for adjacent Node which is Node 3, so
marked it as visited and add up the distance, Now the distance will be:
Distance: Node 0 -> Node 1 -> Node 3 = 2 + 5 = 7
Dijkstra’s Algorithm
Step 4: Again, we have two choices for adjacent Nodes (Either we can choose
Node 4 with distance 10 or either we can choose Node 5 with distance 15) so
choose Node with minimum distance. In this step Node 4 is Minimum distance
adjacent Node, so marked it as visited and add up the distance.
Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 = 2 + 5 + 10 = 17
Dijkstra’s Algorithm
Step 5: Again, Move Forward and check for adjacent Node which is Node 6,
so marked it as visited and add up the distance, Now the distance will be:
Distance: Node 0 -> Node 1 -> Node 3 -> Node 4 -> Node 6 = 2 + 5 + 10 + 2
= 19
Dijkstra’s Algorithm
So, the Shortest Distance from the Source Vertex is 19 which is optimal one
Complexity Analysis:
Time complexity: O((V + E) log V), where V is the number of vertices and E is
the number of edges.
Auxiliary Space: O(V), where V is the number of vertices and E is the number
of edges in the graph.
Floyd-Warshall algorithm
The Floyd-Warshall algorithm, named after its creators Robert Floyd and
Stephen Warshall, is a fundamental algorithm in computer science and graph
theory. It is used to find the shortest paths between all pairs of nodes in a
weighted graph. This algorithm is highly efficient and can handle graphs with
both positive and negative edge weights, making it a versatile tool for solving
a wide range of network and connectivity problems.
NEGATIVE CYCLE: A negative cycle is one in which the overall sum of the
cycle comes negative.
Step 3: Treat node B as an intermediate node and calculate the Distance[][] for
every {i,j} node pair using the formula:
= Distance[i][j] = minimum (Distance[i][j], (Distance from i to B) + (Distance
from B to j ))
= Distance[i][j] = minimum (Distance[i][j], Distance[i][B] + Distance[B][j])
Step 4: Treat node C as an intermediate node and calculate the Distance[][] for
every {i,j} node pair using the formula:
= Distance[i][j] = minimum (Distance[i][j], (Distance from i to C) + (Distance
from C to j ))
= Distance[i][j] = minimum (Distance[i][j], Distance[i][C] + Distance[C][j])
Step 5: Treat node D as an intermediate node and calculate the Distance[][] for
every {i,j} node pair using the formula:
= Distance[i][j] = minimum (Distance[i][j], (Distance from i to D) + (Distance
from D to j ))
= Distance[i][j] = minimum (Distance[i][j], Distance[i][D] + Distance[D][j])
Step 6: Treat node E as an intermediate node and calculate the Distance[][] for
every {i,j} node pair using the formula:
= Distance[i][j] = minimum (Distance[i][j], (Distance from i to E) + (Distance
from E to j ))
= Distance[i][j] = minimum (Distance[i][j], Distance[i][E] + Distance[E][j])
Step 7: Since all the nodes have been treated as an intermediate node, we can
now return the updated Distance[][] matrix as our answer matrix.
Why Floyd-Warshall Algorithm better for Dense Graphs and not for Sparse
Graphs?
Dense Graph: A graph in which the number of edges is significantly much
higher than the number of vertices.
Sparse Graph: A graph in which the number of edges is very much low.
No matter how many edges are there in the graph the Floyd Warshall
Algorithm runs for O(V3) times therefore it is best suited for Dense graphs.
Complexity Analysis:
Time Complexity: O(V3), where V is the number of vertices in the graph and
we run three nested loops each of size V
Auxiliary Space: O(V2), to create a 2-D matrix in order to store the shortest
distance for each pair of nodes.
Comparison
Basis Dijkstra Algorithm Floyd Warshall Algorithm
The Algorithm
the first node in the topological ordering is selected that node which can't have any incoming
directed edges; it must have an indegree of zero. Because if it had incoming directed edges, then
the nodes pointing to it would have to come first.
So, we'll find a node with an indegree of zero and add it to the topological ordering. Once a node
is added to the topological ordering, we can take the node, and its outgoing edges, out of the
graph. Then, we can repeat our earlier approach: look for any node with an indegree of zero and
add it to the ordering. We’ll keep looping until there aren't any more nodes with indegree zero.
This could happen for two reasons:
There are no nodes left. We've taken all of them out of the graph and added them to the
topological ordering.
There are some nodes left, but they all have incoming edges. This means the graph has a
cycle, and no topological ordering exists.