0% found this document useful (0 votes)
10 views

ads unit 3

A graph is a data structure consisting of vertices connected by edges, represented mathematically as a pair of sets (V, E). Key operations include adding vertices and edges, while traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS) are used to explore the graph. Additionally, spanning trees are subsets of graphs that connect all vertices with the minimum number of edges, with algorithms like Kruskal's and Prim's used to find minimum spanning trees.

Uploaded by

aabina321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

ads unit 3

A graph is a data structure consisting of vertices connected by edges, represented mathematically as a pair of sets (V, E). Key operations include adding vertices and edges, while traversal techniques such as Depth First Search (DFS) and Breadth First Search (BFS) are used to explore the graph. Additionally, spanning trees are subsets of graphs that connect all vertices with the minimum number of edges, with algorithms like Kruskal's and Prim's used to find minimum spanning trees.

Uploaded by

aabina321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Data Structure - Graph Data Structure

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 −

In the above graph,


V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}

Graph Data Structure


Mathematical graphs can be represented in data structure. We can represent a graph using
an array of vertices and a two-dimensional array of edges. Before we proceed further, let's
familiarize ourselves with some important terms −
 Vertex − Each node of the graph is represented as a vertex. In the following
example, the labeled circle represents vertices. Thus, A to G are vertices. We can
represent them using an array as shown in the following image. Here A can be
identified by index 0. B can be identified using index 1 and so on.
 Edge − Edge represents a path between two vertices or a line between two vertices.
In the following example, the lines from A to B, B to C, and so on represents
edges. We can use a two-dimensional array to represent an array as shown in the
following image. Here AB can be represented as 1 at row 0, column 1, BC as 1 at
row 1, column 2 and so on, keeping other combinations as 0.
 Adjacency − Two node or vertices are adjacent if they are connected to each other
through an edge. In the following example, B is adjacent to A, C is adjacent to B,
and so on.
 Path − Path represents a sequence of edges between the two vertices. In the
following example, ABCD represents a path from A to D.

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

Graph traversal is a technique used for a searching


vertex in a graph.

The graph traversal is also used to decide the order of


vertices is visited in the search process.

A graph traversal finds the edges to be used in the


search process without creating loops. That means using
graph traversal we visit all the vertices of the graph
without getting into looping path.

There are two graph traversal techniques and they are as


follows...

1. DFS (Depth First Search)


2. BFS (Breadth First Search)
Data Structure - Depth First Traversal
Depth First Search (DFS) algorithm traverses a graph in a depthward
motion and uses a stack to remember to get the next vertex to start a
search, when a dead end occurs in any iteration.

As in the example given above, DFS algorithm traverses from S to A to


D to G to E to B first, then to F and lastly to C. It employs the following
rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Push it in a
stack.

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

 Rule 3 − Repeat Rule 1 and Rule 2 until the stack is empty.

Step Traversal Description

Initialize the stack.


2
Mark S as visited and put it
onto the stack. Explore any
unvisited adjacent node
from S. We have three
nodes and we can pick any
of them. For this example,
we shall take the node in an
alphabetical order.

Mark A as visited and put it


onto the stack. Explore any
unvisited adjacent node from
A. Both Sand D are adjacent
to A but we are concerned
for unvisited nodes only.

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

We check the stack top for


return to the previous node
and check if it has any
unvisited nodes. Here, we
find D to be on the top of
the stack.

Only unvisited adjacent node


is from D is C now. So we
visit C, mark it as visited and
put it onto the stack.

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.

As in the example given above, BFS algorithm traverses from A to B to


E to F first then to C and G lastly to D. It employs the following rules.
 Rule 1 − Visit the adjacent unvisited vertex. Mark it as visited. Display it. Insert it in
a queue.

 Rule 2 − If no adjacent vertex is found, remove the first vertex from the queue.

 Rule 3 − Repeat Rule 1 and Rule 2 until the queue is empty.

Step Traversal Description

Initialize the queue.


2

We start from
visiting S(starting node),
and mark it as visited.

We then see an unvisited


adjacent node from S. In
this example, we have three
nodes but alphabetically we
choose A, mark it as visited
and enqueue it.

Next, the unvisited adjacent


node from S is B. We mark it
as visited and enqueue it.

Next, the unvisited adjacent


node from S is C. We mark it
as visited and enqueue it.
6

Now, S is left with no


unvisited adjacent nodes.
So, we dequeue and find A.

From A we have D as
unvisited adjacent node. We
mark it as visited and
enqueue it.

At this stage, we are left with no unmarked (unvisited) nodes. But as


per the algorithm we keep on dequeuing in order to get all unvisited
nodes. When the queue gets emptied, the program is over.
Data Structure & Algorithms - Spanning Tree

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.

General Properties of Spanning Tree


We now understand that one graph can have more than one spanning tree. Following are a few properties of the spanning tree connected to graph
G−

A connected graph G can have more than one spanning tree.


All possible spanning trees of graph G, have the same number of edges and vertices.
The spanning tree does not have any cycle (loops).

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.

Mathematical Properties of Spanning Tree


Spanning tree has n-1 edges, where n is the number of nodes (vertices).

From a complete graph, by removing maximum e - n + 1 edges, we can construct a spanning tree.

A complete graph can have maximum nn-2 number of spanning trees.

Thus, we can conclude that spanning trees are a subset of connected Graph G and disconnected graphs do not have spanning tree.

Application of Spanning Tree


Spanning tree is basically used to find a minimum path to connect all nodes in a graph. Common application of spanning trees are −

Civil Network Planning

Computer Network Routing Protocol

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.

Minimum Spanning Tree (MST)

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.

Minimum Spanning-Tree Algorithm


We shall learn about two most important spanning tree algorithms here −

Kruskal's Algorithm
1/2
Prim's Algorithm

Both are greedy algorithms.

Video

Azure Data Lake Online Training

42 Lectures 1.5 hours

Ravi Kiran

More Detail

Video

Data Structure Online Training

141 Lectures 13 hours

Arnab Chakraborty

More Detail

Video

Oracle Data Guard Online Training

26 Lectures 8.5 hours

Parth Panjabi

More Detail

Video

Big Data & Hadoop Online Training

65 Lectures 6 hours

Arnab Chakraborty

More Detail

Video

Python With Data Science

75 Lectures 13 hours

Eduonix Learning Solutions

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.

To understand Kruskal's algorithm let us consider the following example −

Step 1 - Remove all loops and Parallel Edges

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.

Step 2 - Arrange all edges in their increasing order of weight


The next step is to create a set of edges and weight, and arrange them in an ascending order of weightage (cost).

Step 3 - Add the edge which has the least weightage

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.

Useful Video Courses


Video

Azure Data Lake Online Training

42 Lectures 1.5 hours

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 −

Step 1 - Remove all loops and parallel edges

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.

Step 2 - Choose any arbitrary node as root node

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.

Useful Video Courses


Video

Azure Data Lake Online Training

42 Lectures 1.5 hours

Ravi Kiran

More Detail

Video

Data Structure Online Training

141 Lectures 13 hours

Arnab Chakraborty

More Detail

Video

Oracle Data Guard Online Training

26 Lectures 8.5 hours

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

using dynamic programming.

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.

Let’s fill in the rest of the fields.


 From 1 to 4, A0(1,4) = 5

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

Next, plug in the values for those entries.

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.

The minimum is 6 so we update A2(1,3) with 6. A2(1,4) is next.


The minimum is 5 so we update A2(1,4) with 5. A2(2,1) is next.

The value is still infinity, so we update A2(2,1) with ∞. A2(2,2) 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.

S.No Dijkstra's Algorithm Bellman-Ford Algorithm


Single source shortest path algorithm. Single source shortest path algorithm.
1

Works when there is a negative edge.


2 Doesn't work when there are negative edges.
Can also detect negative weight cycle.

Result contains vertices which contains


Result contains the vertices containing whole
3 the information about the other vertices
information about the network.
they are connected to.

Cannot be implemented easily in a distributed Can be easily implemented in a


4
way. distributed way.

5 Time complexity is O(ElogV). Time complexity Is O(VE).

Greedy approach is taken to implement the Dynamic programming approach is taken


6
algorithm. to implement the algorithm.

You might also like