Graph: By: Deepak Kumar Singh
Graph: By: Deepak Kumar Singh
you post a photo, join a group, like a page, etc., a new edge is
created for that relationship.
All of facebook is then a collection of these nodes and edges.
This is because facebook uses a graph data structure to store its
data.
More precisely, a graph is a data structure (V, E) that consists of
◦ A collection of vertices V
◦ A collection of edges E, represented as ordered pairs of vertices (u,v)
In the graph
◦ V = {0, 1, 2, 3}
◦ E = {(0,1), (0,2), (0,3), (0,4)}
◦ G = {V, E}
Graph Terminology
Adjacency: A vertex is said to be adjacent to another vertex if
vertex B is called a path. 0-1, 1-2 and 0-2 are paths from vertex 0
to vertex 2.
Directed Graph: A graph in which an edge (u,v) doesn't
Sequential Representation
i) Adjacency Matrix/Bit Matrix/Boolean Matrix
Adjacency matrix A for a graph G = (V , E) with n vertices is an
of the graph.
There are two standard methods by using which, we can traverse
the graphs.
Lets discuss each one of them in detail.
takes a graph as input and finds the subset of the edges of that
graph which
◦ form a tree that includes every vertex
◦ has the minimum sum of weights among all the trees that can
be formed from the graph
How Kruskal's algorithm works
It falls under a class of algorithms called greedy algorithms that find the
Choose the edge with the least weight, if there are more than 1,
choose anyone
Choose the next shortest edge and add it
Choose the next shortest edge that doesn't create a cycle and add
it
Choose the next shortest edge that doesn't create a cycle and add
it
problems. It's because it always goes for the local best choice to
produce the global best result.
However, we can determine if the algorithm can be used with
all cases).
Drawback of Greedy Approach
As mentioned earlier, the greedy algorithm doesn't always produce the
below from root to leaf. Let's use the greedy algorithm here.
Apply greedy approach to this tree to find the longest route
Greedy Approach
1. Let's start with the root node 20. The weight of the right child
is 3 and the weight of the left child is 2.
2. Our problem is to find the largest path. And, the optimal solution
at the moment is 3. So, the greedy algorithm will choose 3.
3. Finally the weight of an only child of 3 is 1. This gives us our
final result 20 + 3 + 1 = 24.
However, it is not the optimal solution. There is another path that
carries more weight (20 + 2 + 10 = 32) as shown in the image below.
Longest path
Therefore, greedy algorithms do not always give an
optimal/feasible solution
Greedy Algorithm
To begin with, the solution set (containing answers) is empty.
At each step, an item is added to the solution set until a solution
is reached.
If the solution set is feasible, the current item is kept.
Else, the item is rejected and never considered again.
Always select the coin with the largest value (i.e. 5) until the sum > 18.
(When we select the largest value at each step, we hope to reach the
destination faster. This concept is called greedy choice property.)
In the first iteration, solution-set = {5} and sum = 5.
{5, 5, 5, 2, 1}.
Assignment
◦ Warshall‟s Algorithm
◦ Round robin algorithm