Greedy algorithms
Greedy algorithms work in phases
In each phase, a decision is made
that appears to be good, without regard for future consequences
Generally, this means that some local
optimum is chosen
When the algorithms terminates, we
hope that the local optimum is equal to the global optimum
This is not always the case;
sometimes greedy algorithms produce suboptimal solutions.
It is important to know whether a
certain greedy algorithm solution is optimal.
Spanning trees
Definition of a spanning tree
Let G be a simple graph. A
spanning tree of G is a subgraph of G that is a tree containing every
vertex of G
A tree is defined as a undirected
graph with no simple circuits
There is always one less edge in a
spanning tree than there are vertices
A spanning tree can be made from a
graph by removing edges that create circuit
This is the easiest way for
people, looking a a graph to make a spanning tree
But it is very difficult for a computer,
since it must continually check for circuits, before it decides which edge to
remove
So computers build spanning trees
by adding edges, not subtracting them
Minimum Spanning Trees
A minimum spanning tree
contains all the vertices in G and minimizes the sum of the weights of the
edges.
We know that the graph G = (V, E)
where V is a set of vertices, and E is a set of edges
A spanning tree of G has the same
set of vertices, but a subset of the set of edges
If we call the subset of the set
of edges F, then the spanning tree T is T = (V, F)
The problem then is to find the
subset F such that
T = (V, F) is a minimum spanning tree
So we need these two sets, V and F. We start with set V
containing one vertex, and set F empty.
We add edges and vertices until
all the vertices are added.
Finding minimum spanning trees
The greedy method of problem
solving is used to find the MST from a graph G
There are two common algorithms,
both greedy, that differ by how a minimum edge is selected.
Kruskals algorithm
Continually select the smallest edge, unless it
creates a cycle with the other edges already selected. (several clusters that
may get connected only at the end)
Prims algorithm
Pick a starting vertex, and add a minimum adjacent
edge and vertex at each stage. (no cycles)
Implementing Prims algorithm
Data structures used:
the adjacency list where the
graph is stored
A 1-D array F that stores the edges and weight between them as they are added to the
MST
It is
initialized empty
A 1-D array Y that stores the vertices that have been included in the MST
Y is initialized
with the starting vertex
The nearest vertex adjacent to any
vertices in Y is added to Y and the edge is
added to F
Repeated until Y = V (V is the set of all vertices).
Ties are broken arbitrarily
Taking the vertex from V Y guarantees that a cycle is not created
Time complexity of Prims algorithm
For each edge put into the MST, we must find the
shortest edge adjacent to any of the vertices that are marked
This requires a nested loop.
So the time complexity is O(n2)
Kruskals algorithm
Choose the edge with the smallest
weight, and mark the vertex at each end as processed, and add the edge to the
MST
Ties are broken arbitrarily
Choose the next smallest edge,
If it does not create a cycle, add
it to the MST
If neither of its terminal
vertices is marked, it will not create a cycle
Mark the vertex (or vertices) as
processed
Continue until there is one less
edge than vertex