Professional Documents
Culture Documents
6.1 Spanning Trees - AlfredBarbosa - MarkEmanDigdigan - Manalo
6.1 Spanning Trees - AlfredBarbosa - MarkEmanDigdigan - Manalo
Herwin Manalo
Mark Emman Digdigan
Alfred Barbosa
For example, maybe a group of people are taking a trip and begin at node 5. They may choose
option 6 during the trip, which leads them to options 8, 7, and 9 (i.e., specific destinations). Or,
they could choose option 2, which leads them to a separate set of possible destinations. Also,
notice that there is only one path between each option in the tree. In other words, there is only
one way to reach destination 7 (by first choosing destination 8). If someone chooses a route that
does not include node 8, it is not possible to arrive at destination 7. This is what it means to be
acyclic.
An example of a basic number tree
The numbers in each node can be used to represent discrete categories, such as events that can
occur or the outcomes of various decisions that someone can make.
UNDIRECTED GRAPHS
An undirected graph is a graph in which the edges do not point in any direction (ie. the edges are
bidirectional).
CONNECTED GRAPHS
A connected graph is a graph in which there is always a path from a vertex to any other vertex.
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.
The edges may or may not have weights assigned to them.
The total number of spanning trees with n vertices that can be created from a complete graph is
equal to n(n-2).
If we have n = 4, the maximum number of possible spanning trees is equal to 44-2 = 16. Thus, 16
spanning trees can be formed from a complete graph with 4 vertices.
WEIGHTED
GRAPH
Using a Stack
An easier way to perform the search:
Depth-First Practice
Breadth-First Search
Breadth-first implies we search horizontally before vertically.
1. Choose a “root” vertex for a rooted graph
2. Add all edges incident to that vertex. The newly-connected vertices are level 1 in the
spanning tree.
3. From each vertex in level 1, in order, add all edges incident to the vertices that are not
already included in the spanning tree.
4. Repeat adding levels and edges until all vertices have been visited.
Breadth-First Practice
The minimum spanning tree from a graph is found using the following algorithms:
Prim's Algorithm
Prim's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds the
subset of the edges of that graph which form a tree that includes every vertex and has the
minimum sum of weights among all the trees that can be formed from the graph
Kruskal's Algorithm
Kruskal's algorithm is a minimum spanning tree algorithm that takes a graph as input and finds
the subset of the edges of that graph which form a tree that includes every vertex and has the
minimum sum of weights among all the trees that can be formed from the graph
Prim's Algorithm
It falls under a class of algorithms called greedy algorithms that find the local optimum in the
hopes of finding a global optimum.
We start from one vertex and keep adding edges with the lowest weight until we reach our goal.
The steps for implementing Prim's algorithm are as follows:
Initialize the minimum spanning tree with a vertex chosen at random.
Find all the edges that connect the tree to new vertices, find the minimum and add it to
the tree
Keep repeating step 2 until we get a minimum spanning tree
EXAMPLES:
Kruskal’s Algorithm
It falls under a class of algorithms called greedy algorithms that find the local optimum in the
hopes of finding a global optimum.
We start from the edges with the lowest weight and keep adding edges until we reach our goal.
The steps for implementing Kruskal's algorithm are as follows:
Sort all the edges from low weight to high
Take the edge with the lowest weight and add it to the spanning tree. If adding the edge
created a cycle, then reject this edge.
Keep adding edges until we reach all vertices.
EXAMPLES