ads unit 3
ads unit 3
A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed
as vertices, and the links that connect the vertices are called edges.
Formally, a graph is a pair of sets (V, E), where V is the set of vertices and E is the set of
edges, connecting the pairs of vertices. Take a look at the following graph −
Basic Operations
Following are basic primary operations of a Graph −
Add Vertex − Adds a vertex to the graph.
Add Edge − Adds an edge between the two vertices of the graph.
Display Vertex − Displays a vertex of the graph.
Graph Traversals
Rule 2 − If no adjacent vertex is found, pop up a vertex from the stack. (It will pop
up all the vertices from the stack, which do not have adjacent vertices.)
4
Visit D and mark it as visited
and put onto the stack.
Here, we
have B and C nodes, which
are adjacent to D and both
are unvisited. However, we
shall again choose in an
alphabetical order.
We choose B, mark it as
visited and put onto the
stack. Here Bdoes not have
any unvisited adjacent node.
So, we pop Bfrom the stack.
6
As C does not have any unvisited adjacent node so we keep popping the
stack until we find a node that has an unvisited adjacent node. In this
case, there's none and we keep popping until the stack is empty.
Data Structure - Breadth First Traversal
Breadth First Search (BFS) algorithm traverses a graph in a
breadthward motion and uses a queue to remember to get the next
vertex to start a search, when a dead end occurs in any iteration.
Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.
We start from
visiting S(starting node),
and mark it as visited.
From A we have D as
unvisited adjacent node. We
mark it as visited and
enqueue it.
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..
By this definition, we can draw a conclusion that 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.
We found three spanning trees off one complete graph. A complete undirected graph can have maximum nn-2 number of spanning trees, where n
is the number of nodes. In the above addressed example, n is 3, hence 33−2 = 3 spanning trees are possible.
Removing one edge from the spanning tree will make the graph disconnected, i.e. the spanning tree is minimally connected.
Adding one edge to the spanning tree will create a circuit or loop, i.e. the spanning tree is maximally acyclic.
From a complete graph, by removing maximum e - n + 1 edges, we can construct a spanning tree.
Thus, we can conclude that spanning trees are a subset of connected Graph G and disconnected graphs do not have spanning tree.
Cluster Analysis
Let us understand this through a small example. Consider, city network as a huge graph and now plans to deploy telephone lines in such a way
that in minimum lines we can connect to all city nodes. This is where the spanning tree comes into picture.
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's Algorithm
1/2
Prim's Algorithm
Video
Ravi Kiran
More Detail
Video
Arnab Chakraborty
More Detail
Video
Parth Panjabi
More Detail
Video
65 Lectures 6 hours
Arnab Chakraborty
More Detail
Video
75 Lectures 13 hours
2/2
Kruskal's Spanning Tree Algorithm
Kruskal's algorithm to find the minimum cost spanning tree uses the greedy approach. This algorithm treats the graph as a forest and every node it
has 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.
Remove all loops and parallel edges from the given graph.
In case of parallel edges, keep the one which has the least cost associated and remove all others.
Now we start adding edges to the graph beginning from the one which has the least weight. Throughout, we shall keep checking that the spanning
properties remain intact. In case, by adding one edge, the spanning tree property does not hold then we shall consider not to include the edge in
the graph.
The least cost is 2 and edges involved are B,D and D,T. We add them. Adding them does not violate spanning tree properties, so we continue to
our next edge selection.
1/2
Next cost is 3, and associated edges are A,C and C,D. We add them again −
Next cost in the table is 4, and we observe that adding it will create a circuit in the graph. −
We ignore it. In the process we shall ignore/avoid all edges that create a circuit.
We observe that edges with cost 5 and 6 also create circuits. We ignore them and move on.
Now we are left with only one node to be added. Between the two least cost edges available 7 and 8, we shall add the edge with cost 7.
By adding edge S,A we have included all the nodes of the graph and we now have minimum cost spanning tree.
Ravi Kiran
More Detail
Video
2/2
Prim's Spanning Tree Algorithm
Prim's algorithm to find minimum cost spanning tree (as Kruskal's algorithm) uses the greedy approach. Prim's algorithm shares a similarity with
the shortest path first algorithms.
Prim's algorithm, in contrast with Kruskal's algorithm, treats the nodes as a single tree and keeps on adding new nodes to the spanning tree from
the given graph.
To contrast with Kruskal's algorithm and to understand Prim's algorithm better, we shall use the same example −
Remove all loops and parallel edges from the given graph. In case of parallel edges, keep the one which has the least cost associated and remove
all others.
In this case, we choose S node as the root node of Prim's spanning tree. This node is arbitrarily chosen, so any node can be the root node. One
may wonder why any video can be a root node. So the answer is, in the spanning tree all the nodes of a graph are included and because it is
connected then there must be at least one edge, which will join it to the rest of the tree.
Step 3 - Check outgoing edges and select the one with less cost
After choosing the root node S, we see that S,A and S,C are two edges with weight 7 and 8, respectively. We choose the edge S,A as it is lesser
than the other.
Now, the tree S-7-A is treated as one node and we check for all edges going out from it. We select the one which has the lowest cost and include it
in the tree.
1/2
After this step, S-7-A-3-C tree is formed. Now we'll again treat it as a node and will check all the edges again. However, we will choose only the
least cost edge. In this case, C-3-D is the new edge, which is less than other edges' cost 8, 6, 4, etc.
After adding node D to the spanning tree, we now have two edges going out of it having the same cost, i.e. D-2-T and D-2-B. Thus, we can add
either one. But the next step will again yield edge 2 as the least cost. Hence, we are showing a spanning tree with both edges included.
We may find that the output spanning tree of the same graph using two different algorithms is same.
Ravi Kiran
More Detail
Video
Arnab Chakraborty
More Detail
Video
Parth Panjabi
2/2
All-Pairs Shortest Path Matrix Multiplication
Y ou’re presented with a graph and your goal is to find all-pairs of shortest paths
The first step is to create a matrix where the number of rows and columns equals the
number of vertices and then to populate it with initial data. For each vertex that can
be reached by one hop, you will populate it with the edge weight. If a vertex cannot be
reached with one hop, you will populate it with infinity and if a vertex is trying to
reach itself, you will populate it with zero. Let’s walk through the algorithm to
populate the initial matrix.
We’ll start by populating all the zeros. The zeros will be down the diagonal. Why?
Looking at the first entry at A1(1,1), you’re trying to go from vertex 1 to vertex 1.
What’s the weight of that? Zero. Same occurs for (2,2), (3,3), and (4,4).
Let’s examine A1(1,2) next. You can get from vertex 1 to 2 in one hop. The edge weight
is 10.
Next, we’ll look at A1(1,3). Since you cannot get to vertex 3 from vertex 1 in one hop,
you enter infinity.
From 2 to 1, A0(2,1) = ∞
From 2 to 3, A0(2,3) = ∞
From 2 to 4, A0(2,4) = 9
From 3 to 1, A0(3,1) = -2
From 3 to 2, A0(3,2) = 4
From 3 to 4, A0(3,4) = ∞
From 4 to 1, A0(4,1) = ∞
From 4 to 2, A0(4,2) = -3
From 4 to 3, A0(4,3) = 1
Next, let’s look at the algorithm programmatically and visually.
We start with trying to obtain the value for A0(1,1). If we follow the for loop, k will be
equal to 1, then 2, then 3 and finally 4 while the values of i and j do not change.
The minimum value is 0. So, we take the value of zero and plug it into A1(1,1)
Next, we’ll examine A1(1,2), but this time we’ll do it visually. What we did in the
previous example is go through the row and the column corresponding to the entry.
Since we looked at entry (1,1), we added the values from the first row and the first
column and then got the minimum of those entries. Let’s start off by highlighting the
first row and second column since we’re working on entry (1,2).
If we walk through the algorithm we add (1,1) to (1,2), (1,2) to (2,2), (1,3) to (3,2) and
(1,4) to (4,2).
The minimum is 2 so we update A2(1,2) with 2.
Let’s fill out the rest of the cells visually as well for the first row. A2(1,3) is next.
For the remainder of the iteration in constructing A2, I’ll list the items and you can
verify them by following through the matrix A1.
The final product looks like the following:
We keep repeating the procedure until we’re able to observe one of two occurrences:
The entries from the previous matrix to the current matrix don’t change
There is a negative value in the diagonal. This indicates a negative cycle and the
values will decrease indefinitely.
What exactly is the A2 matrix? It’s the result of modified matrix multiplication of two
A1 matrices. The next matrix to find is A4. That will be accomplished by multiplying
two A2 matrices. Let’s run through the entire process.
Since the matrix changed from the previous version, we’re still not finished. Time to
move to A8.
Since there are no changes between A4 and A8, no further action is necessary.
Difference between Dijkstra’s and Bellman-Ford Algorithm.