DATA STRUCTURES
CSO102
SPANNING TREE:
– A tree (i.e., connected, acyclic graph) which
contains all the vertices of the graph
- How many spanning trees will be there in a
complete graph with n vertices?
SPANNING FOREST:
– If a graph is not connected, then there is a
spanning tree for each connected component
of the graph
MINIMUM SPANNING TREE:
– Spanning tree with the minimum sum of
weights
APPLICATIONS OF MINIMUM SPANNING TREES:
– Find the least expensive way to connect a
set of cities, terminals, computers, etc.
Example:
- A town has a set of houses and a set of roads.
- A road connects 2 and only 2 houses.
- A road connecting houses u and v has a repair cost w(u, v).
Goal: Repair enough (and no more) roads such that:
1. Everyone stays connected
i.e., can reach every house from all other houses
2. Total repair cost is minimum
KRUSKAL’S ALGORITHM:
1. Sort all the edges in non-decreasing order of
their weight.
2. Pick the smallest edge. Check if it forms a cycle
with the spanning tree formed so far. If the cycle
is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the
spanning tree.
KRUSKAL’S ALGORITHM:
KRUSKAL’S ALGORITHM:
A={}
PRIM’S ALGORITHM:
1: Determine an arbitrary vertex as the
starting vertex of the MST.
2: Follow steps 3 to 5 till there are vertices
that are not included in the MST (known as
fringe vertex).
3: Find edges connecting any tree vertex
with the fringe vertices.
4: Find the minimum among these edges.
5: Add the chosen edge to the MST if it does
not form any cycle.
6: Return the MST and exit
PRIM’S ALGORITHM:
PRIM’S ALGORITHM:
SHORTEST PATH PROBLEM:
● Single source shortest path problem
● All pair shortest path problem
DIJKSTRA’S ALGORITHM:
● It supports only non-negative weights
● It can be used for both directed graphs and undirected graphs
● It uses greedy strategy to compute the shortest path, although the
answer obtained is proved to be correct
DIJKSTRA’S ALGORITHM:
DIJKSTRA’S ALGORITHM:
DIJKSTRA’S ALGORITHM:
DIJKSTRA’S ALGORITHM:
TIME COMPLEXITY?
ALL PAIR SHORTEST PATH SOLUTION USING DIJKSTRA’S
ALGO?
BELLMAN-FORD ALGORITHM:
● Allows negative-weight edges;
● Computes v.d and v.π for all v ∈ V; and
● Returns True (and a solution embedded in the graph) if no
negative-weight cycles are reachable from s, and False otherwise.
BELLMAN-FORD ALGORITHM:
negative cycle
BELLMAN-FORD ALGORITHM:
All-Pairs Shortest Paths- Floyd Warshall Algorithm:
● Initialize the solution matrix same as the input graph matrix as a
first step.
● Then update the solution matrix by considering all vertices as an
intermediate vertex.
● The idea is to pick all vertices one by one and updates all
shortest paths which include the picked vertex as an
intermediate vertex in the shortest path.
● When we pick vertex number k as an intermediate vertex, we
already have considered vertices {0, 1, 2, .. k-1} as intermediate
vertices.
● For every pair (i, j) of the source and destination vertices
respectively, there are two possible cases.
● k is not an intermediate vertex in shortest path from i to j. We
keep the value of dist[i][j] as it is.
● k is an intermediate vertex in shortest path from i to j. We update
the value of dist[i][j] as dist[i][k] + dist[k][j], if dist[i][j] > dist[i][k] +
dist[k][j]
Floyd Warshall Algorithm:
036∞∞∞∞ 0345677
3021∞∞∞ 3021344
620142∞ 4201323
∞1102∞4 5110233
∞∞42021 6332021
∞∞2∞201 7423201
∞∞∞4110 7433110
Input Output
Floyd Warshall Algorithm:
For k = 0 to n – 1
For i = 0 to n – 1
For j = 0 to n – 1
Distance[i, j] = min(Distance[i, j], Distance[i, k] + Distance[k, j])
where i = source Node, j = Destination Node, k = Intermediate
Node
Time Complexity: O(V3), where V is the number of
vertices in the graph and we run three nested loops each of
size V
ARTICULATION POINT:
BICONNECTED GRAPH: If no articulation point
ARTICULATION POINT:
TOPOLOGICAL SORTING:
TOPOLOGICAL SORTING:
- Topological Sorting is applicable only for DAG(Directed Acyclic
Graph). Why is it so?
Because of the following reasons:
● For Undirected graphs ,only u->v is not applicable . It cannot be sure
whether the edge is between u to v or v to u ( u-v ).
● In a cyclic graph there will always be a dependency factor . You
cannot make sure that you can have linear ordering of vertices.
TOPOLOGICAL SORTING:
TRANSITIVE CLOSURE OF A DIRECTED GRAPH:
Given a directed graph, find out if a vertex j is reachable from another
vertex i for all vertex pairs (i, j) in the given graph. Here reachable
mean that there is a path from vertex i to j. The reach-ability matrix is
called the transitive closure of a graph.
Transitive closure of above graphs is
1 1 1 1
1 1 1 1
1 1 1 1
0 0 0 1
-> Using DFS?
CONNECTED COMPONENTS OF A DIRECTED
GRAPH:
-> Using DFS and the complement of a graph G