Download presentation
Presentation is loading. Please wait.
1
CSC 380: Design and Analysis of Algorithms
Dr. Curry Guinn
2
Quick Info Dr. Curry Guinn CIS 2015 guinnc@uncw.edu
Office Hours: MWF: 10:00am-11:00m and by appointment
3
Outline of today Closest Points Greedy Algorithms Making change
Minimum spanning tree Prim Kruskal
5
Optimization problems
An optimization problem is one in which you want to find, not just a solution, but the best solution A “greedy algorithm” sometimes works well for optimization problems A greedy algorithm works in phases. At each phase: You take the best you can get right now, without regard for future consequences You hope that by choosing a local optimum at each step, you will end up at a global optimum 2
6
Example: Counting money
Suppose you want to count out a certain amount of money, using the fewest possible bills and coins A greedy algorithm would do this would be: At each step, take the largest possible bill or coin that does not overshoot Example: To make $6.39, you can choose: a $5 bill a $1 bill, to make $6 a 25¢ coin, to make $6.25 A 10¢ coin, to make $6.35 four 1¢ coins, to make $6.39 For US money, the greedy algorithm always gives the optimum solution 3
7
A failure of the greedy algorithm
In some (fictional) monetary system, “krons” come in 1 kron, 7 kron, and 10 kron coins Using a greedy algorithm to count out 15 krons, you would get A 10 kron piece Five 1 kron pieces, for a total of 15 krons This requires six coins A better solution would be to use two 7 kron pieces and one 1 kron piece This only requires three coins The greedy algorithm results in a solution, but not in an optimal solution 4
8
Other greedy algorithms
Prim’s algorithm for finding a minimum-cost spanning tree Always takes the lowest-cost edge between nodes in the spanning tree and nodes not yet in the spanning tree Kruskal’s algorithm for finding a minimum-cost spanning tree Always tries the lowest-cost remaining edge Dijkstra’s algorithm for finding the shortest path in a graph Always takes the shortest edge connecting a known node to an unknown node
9
Minimum spanning tree Tree: a connected, directed acyclic graph
Spanning tree: a subgraph of a graph, which meets the constraints to be a tree (connected, acyclic) and connects every vertex of the original graph Minimum spanning tree: a spanning tree with weight less than or equal to any other spanning tree for the given graph
10
Min. span. tree applications
Consider a cable TV company laying cable to a new neighborhood... Each house is a vertex. Connections between houses have a cost based on length. What is the minimum layout that connects all the houses?
11
Minimum Cost Spanning Trees Spanning Tree
Definition Consider a connected, undirected graph G=(V, E). A spanning tree T = (V’, E’) of G is a subgraph of G with the following properties: V’ = V T is connected T is acyclic
12
Minimum Cost Spanning Trees Total Cost
Definition The total cost of an edge-weighted undirected graph is the sum of the weights on all the edges in that graph
13
Minimum Cost Spanning Trees Minimum Spanning Tree
Definition Consider an edge-weighted, undirected, connected graph G=(V, E), where c(v, w) represents the weight on edge {v, w}E. The minimum spanning tree of G is the spanning tree T=(V, E’) that has the smallest total cost,
14
Minimum Cost Spanning Trees Prim’s Algorithm
Constructs the minimum-cost spanning tree of a graph by selecting edges from the graph one-by-one and adding those edges to the spanning tree At each step, select an edge with the smallest edge weight that connects the tree to a vertex not yet in the tree
15
2 V1 V2 V1 V2 4 1 3 10 1 2 7 V3 V3 V4 V5 V4 V5 8 4 6 5 1 V6 V7 V6 V7
16
2 2 V1 V2 V1 V2 4 1 3 10 1 2 7 V3 V3 V4 V5 V4 V5 8 4 6 5 1 V6 V7 V6 V7
17
2 2 V1 V2 V1 V2 4 1 3 10 1 2 7 2 V3 V3 V4 V5 V4 V5 8 4 6 5 1 V6 V7 V6 V7
18
2 2 V1 V2 V1 V2 4 1 3 10 1 2 7 2 V3 V3 V4 V5 V4 V5 8 4 4 6 5 1 V6 V7 V6 V7
19
2 2 V1 V2 V1 V2 4 1 3 10 1 2 7 2 V3 V3 V4 V5 V4 V5 8 4 4 6 5 1 1 V6 V7 V6 V7
20
2 2 V1 V2 V1 V2 4 1 3 10 1 2 7 2 V3 V3 V4 V5 V4 V5 8 4 4 6 6 5 1 1 V6 V7 V6 V7 Finished!
21
Prim’s algorithm
22
Basics of Kruskal’s Algorithm
Attempts to add edges to A in increasing order of weight (lightest edge first) If the next edge does not induce a cycle among the current set of edges, then it is added to A. If it does, then this edge is passed over, and we consider the next edge in order. As this algorithm runs, the edges of A will induce a forest on the vertices and the trees of this forest are merged together until we have a single tree containing all vertices.
23
Example: Kruskal’s Algorithm
24
Kruskal
25
For Next Class, Friday Dijkstra’s algorithm for finding the shortest path between a source node and a destination node Homework 5 due Tuesday after Spring Break
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.