spanning Tree
spanning Tree
SPANNING TREES
• A spanning tree is a graph G=(V,E) which has all vertices covered with minimum number of edges
• Suppose you have a connected undirected graph
• Connected: every node is reachable from every other node
• Undirected: edges do not have an associated direction
• ...then a spanning tree of the graph is a connected subgraph in which there are no cycles
2
POSSIBLE SPANNING TREE
•If a graph is complete and consists of n vertices then possible number of spanning tree is
𝑛𝑛−2 .
•If graph is not complete then possible number of spanning tree=C(E, |V|-1)-no of cycle in
graph+1
MINIMUM SPANNING TREE (MST)
• There are two basic algorithms for finding minimum-cost spanning trees, and both are
greedy algorithms
• Kruskal’s algorithm: Start with no nodes or edges in the spanning tree, and repeatedly
add the cheapest edge that does not create a cycle
• Here, we consider the spanning tree to consist of edges only
• Prim’s algorithm: Start with any one node in the spanning tree, and repeatedly add the
cheapest edge, and the node it leads to, for which the node is not already in the spanning
tree.
• Here, we consider the spanning tree to consist of both nodes and edges
6
COMPLEXITY OF KRUSKAL ALGORITHM
KRUSKAL(V, E, w)
A← { } //Set A will ultimately contains the edges of the MST
for each vertex v in V O(V)
do MAKE-SET(v)
sort E into nondecreasing order by weight w O(ElogE)
for each (u, v) taken from the sorted list O(E)
do if FIND-SET(u) = FIND-SET(v)
then A ← A ∪ {(u, v)}
UNION(u, v)
return A
PRIM’S ALGORITHM
• Prim’s algorithm treats the node as a single tree and keep on adding new nodes in
spanning tree.
• Remove loop and parallel edges from the graph
• Choose any arbitrary node as root-node
• Prim’s algorithm build the MST by adding leaves one at a time in current spanning
tree.
• At any time, subset of edges A from a single tree (in kruskal it formed a forest)
PRIM’S ALGORITHM
BFS
• Breadth-first search is one of the simplest algorithms for searching a graph and the archetype for many important
graph algorithms.
• Given a graph G = (V.E) and a distinguished source vertex s, breadth-first search systematically explores the edges of G
to “discover” every vertex that is reachable from s.
• It computes the distance (smallest number of edges) from s to each reachable vertex.
• The algorithm discovers all vertices at distance k from s before discovering any vertices at distance k+1.
DFS
• The strategy followed by depth-first search is, as its name implies, to search “deeper” in the graph whenever
possible.
• Depth-first search explores edges out of the most recently discovered v vertex that still has unexplored edges
leaving it.
• Once all of ’s edges have been explored, the search “backtracks” to explore edges leaving the vertex from
which v was discovered.
• This process continues until we have discovered all the vertices that are reachable from the original source
vertex.