Professional Documents
Culture Documents
Real-World Example
Path(A, E) = { AB, BE }
OR
Path(A, E) = { AC, CD, DE }
OR
Path(A, E) = { AD, DE }
1 2 3 5 is a path.
1 2 3 2 1 is not a path, because a node can’t appear twice in
a path.
1 3 5 is not a path, as adjacent nodes must have an edge
and there is no edge between 1 and 3.
6. Undirected Graph: An undirected graph is one where
the edges do not specify a particular direction. The edges
are bi-directional.
Example
Example
fi
Thus, in the same example, now the edges are directional.
You can only traverse the edge along its direction. There is
no path from node B to node C now.
Example
Thus, in the same example, now the edges have a certain
weight associated with them. There are two possible paths
between node A to node E.
Path1 = { AB, BD, DE }, Weight1 = 3+2+5 = 10
Path2 = { AC, CD, DE }, Weight2 = 1+3+5 = 9
Clearly, one would prefer Path2 if the goal is to reach node
E from node A with minimum cost.
Degree of Graph
Example,
D(3) = 3
D(4) = 2
Representing Graphs
• Adjacency matrix
◦ n by n matrix, where n is number of vertices
◦ A(m,n) = 1 i (m,n) is an edge, or 0 otherwise
◦ For weighted graph: A(m,n) = w (weight of edge),
or positive in nity otherwise
ff
fi
• Adjacency list
◦ Each vertex has a linked list of edges
◦ Edge stores destination and label
◦ Better when adjacency matrix is sparse
1. Add/Remove Vertex
Example
fi
Traversing the above graph in BFS fashion would result
from A -> B -> C -> D -> E -> F -> G. The algorithm starts
from node A and traverses all its adjacent nodes B, C and
D. It marks all the four nodes as visited. Once all the
adjacent nodes of A are traversed, the algorithm moves to
the rst adjacent node of A and repeats the same
procedure. In this case, the node is B, and the adjacent
nodes to B are E and F. Next, the adjacent nodes to C are
traversed. Once all the nodes are visited, the operation
ends.
Example
fi
fi
Traversing the above graph in BFS fashion would result
from A -> B -> E -> F -> C -> G -> D. The algorithm starts
from node A and traverses all its child nodes. As soon as it
encounters B, it seems that it has further child nodes. So,
the child nodes of B are traversed before proceeding to the
next child node of A.
BFS algorithm
A standard BFS implementation puts each vertex of the
graph into one of two categories:
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as
visited while avoiding cycles.
Visit last remaining item in the queue to check if it has unvisited neighbors
Since the queue is empty, we have completed the Breadth
First Traversal of the graph.
Depth First Search (DFS)
Depth rst Search or Depth rst traversal is a recursive
algorithm for searching all the vertices of a graph or tree
data structure. Traversal means visiting all the nodes of a
graph.
1. Visited
2. Not Visited
The purpose of the algorithm is to mark each vertex as
visited while avoiding cycles.
After we visit the last element 3, it doesn't have any unvisited adjacent
nodes, so we have completed the Depth First Traversal of the graph.
Spanning Tree and Minimum
Spanning Tree
Before we learn about spanning trees, we need to
understand two graphs: undirected graphs and connected
graphs.
Undirected Graph
A connected graph is a graph in which there is always a
path from a vertex to any other vertex.
Connected Graph
Spanning tree
A spanning tree is a sub-graph of an undirected connected
graph, which includes all the vertices of the graph with a
minimum possible number of edges. If a vertex is missed,
then it is not a spanning tree.
Normal graph
1. Prim's Algorithm
2. Kruskal's Algorithm
Prim's Algorithm
Prim's algorithm is a minimum spanning tree algorithm that
takes a graph as input and nds the subset of the edges of
that graph which
We start from one vertex and keep adding edges with the
lowest weight until we reach our goal.
Choose a vertex
fi
Choose the shortest edge from this vertex and add it
Choose the nearest edge not yet in the solution, if there are multiple choices,
choose one at random
Repeat until you have a spanning tree
Your One-Stop Solution to
Learn Kruskal Algorithm
From Scratch
•
Step 1: Sort all edges in increasing order of their edge
weights.
• Step 2: Pick the smallest edge.
• Step 3: Check if the new edge creates a cycle or loop
in a spanning tree.
• Step 4: If it doesn’t form the cycle, then include that
edge in MST. Otherwise, discard it.
• Step 5: Repeat from step 2 until it includes |V| - 1
edges in MST.
Using the steps mentioned above, you will generate a
minimum spanning tree structure. So, now have a look at
an example to understand this process better.
After this step, you will include edges in the MST such that
the included edge would not form a cycle in your tree
structure. The rst edge that you will pick is edge EF, as it
has a minimum edge weight that is 2.
fi
Add edge FD to the spanning tree.
Following edge CD, you have edge BF. This edge also
creates the loop; hence you will discard it.
Next up is edge BD. This edge also formulates a loop, so
you will discard it as well.
Next on your sorted list is edge AB. This edge does not
generate any cycle, so you need not include it in the MST
structure. By including this node, it will include 5 edges in
the MST, so you don’t have to traverse any further in the
sorted list. The nal structure of your MST is represented in
the image below: