greedy algorithm
DESCRIPTION
Greedy Algorithm. Nattee Niparnan. Greedy. If solving problem is a series of steps Simply pick the one that “maximize” the immediate outcome Instead of looking for the long run result. Example of Greedy Algorithm. Rational Knapsack Step You have to select items to be put into your sack - PowerPoint PPT PresentationTRANSCRIPT
Greedy Algorithm
Greedy AlgorithmNattee NiparnanGreedyIf solving problem is a series of stepsSimply pick the one that maximize the immediate outcomeInstead of looking for the long run resultExample of Greedy AlgorithmRational Knapsack
StepYou have to select items to be put into your sackGreedySimply pick the one having highest price/weight ratio
We know that this approach does not work in the case of 0-1 knapsackMinimal Spanning TreeMinimal Spanning TreeGiven an undirected connected graphWith weighted edgesFind a subgraph of that graphThat is connectedHaving smallest sum of edges weightsExample
graphMSTObservationMST should not have a cycleWhy?
Removing edge in a cycle does not destroy the connectivity problemSo why bother having an edge in a cycle in the MSTProperty of TreesA tree with N nodes has N 1 edges
A connected graph having |V| - 1 = |E| is a treeMST ProblemInputAn undirected connected graphWeighted edgesOutputA set of edges that constitute the MST of the given graph
(notice that all nodes must be in the tree, so we dont need to select them)Kruskals AlgorithmIdeaWe need |V|- 1 edgesSimply pick them
At each step, just select one edge having smallest valueThat does not cause a cycle
Example
ACEBDFExample
ACEBDF1Example
ACEBDF11Example
ACEBDF111Example
ACEBDF1112Example
ACEBDF11123Cut PropertyWhy Kruskals works?
It is because of the cut propertySuppose a set of edges X are part of a MST of G = (V,E), pick any subset of nodes S for which X does not cross between S and V S, and let e be the lightest edge across this partition. Then X U {e} is part of some MST
Cut Property
Implementing KruskalsNeed something to check the connected component of the graphWe could do CC problemBut that takes too much time
Try some data structure that represent disjoint setIt should be able tomakeset(x)create a set containing just xfind(x)find a set where x is a memberunion(x,y)union a set find(x) and a set find(y)Implementing Kruskalsprocedure kruskal(G;w)//Input: A connected undirected graph G = (V,E) with edge weights we//Output: A minimum spanning tree defined by the edges X
for all u V : makeset(u)
X = {}Sort the edges E by weightfor all edges (u,v) E, in increasing order of weight: if find(u) != find(v): add edge (u,v) to X union(u, v)AnalysisWhat is O of kruskal?It sorts the edgesIt needs |V| makeset2|E| find|V| - 1 union
What is the eventual complexity?Prims AlgorithmInstead of selecting the minimal edge of all edges that does not create a cycleSelect the minimal edge of all edges that connects to the original graphPrims Implementationprocedure kruskal(G;w)//Input: A connected undirected graph G = (V,E) with edge weights we//Output: A minimum spanning tree defined by the array prevfor all u V : cost(u) = 1 prev(u) = nilPick any initial node u0cost(u0) = 0H = makequeue(V ) (priority queue, using cost-values as keys)while H is not empty: v = deletemin(H) for each (v,z) E: if cost(z) > w(v,z): cost(z) = w(v,z) prev(z) = vExample
ACEBDFExample
ACEBDF1Example
ACEBDF12Example
ACEBDF112Example
ACEBDF1123Example
ACEBDF11123