Optimization and Greedy Algorithms

Transcrição

Optimization and Greedy Algorithms
Fachhochschule Braunschweig / Wolfenbüttel
- University of Applied Sciences -
Data Structures
and Algorithm Design
- CSCI 340 Friedhelm Seutter
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
© Friedhelm Seutter, 2007
Contents
1.
2.
3.
4.
6.
7.
8.
10.
13.
Analyzing Algorithms and Problems
Data Abstraction
Recursion and Induction
Sorting
Dynamic Sets and Searching
Graphs and Graph Traversals
Optimization and Greedy Algorithms
Dynamic Programming
NP-Complete Problems
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
2
© Friedhelm Seutter, 2007
1
8. Optimization and Greedy Algorithms
• Minimum spanning Trees
• Shortest Paths
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
3
© Friedhelm Seutter, 2007
Minimum Spanning Tree
• Let G = (V,E) be a connected, undirected,
weighted graph with weight function
w : E → IR.
• A tree G’ = (V,T) with edges T ⊆ E which
connect all vertices and whose total weight
w(T) = ∑ (u,v)∈T w(u,v)
is minimized is called a minimum spanning
tree of G.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
4
© Friedhelm Seutter, 2007
2
Optimization Problem
• A problem with many possible solutions is
called an optimization problem, if an optimal
solution under a given valuation function is to
find.
• An optimal solution may be a minimized or a
maximized value of the given function.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
5
© Friedhelm Seutter, 2007
Optimization Problem
• First try algorithm (in general):
Compute all solutions and then decide which
is optimal.
• Minimum spanning tree:
Compute all spanning trees and supply the
one with minimum weight.
• Not efficient: exponential number of
alternatives
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
6
© Friedhelm Seutter, 2007
3
Greedy Algorithm
• Type of algorithm which solves optimization
problems efficiently.
• It starts with an empty subsolution and
expands this iteratively with the present best
alternative.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
7
© Friedhelm Seutter, 2007
Greedy Algorithm
• A local optimal decision should lead to a
global optimal solution.
• But an optimal solution is not found in all
cases, sometimes only “good” solutions are
found.
• The algorithm in each step “takes the biggest
piece”, therefore it is called “greedy”.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
8
© Friedhelm Seutter, 2007
4
Kruskal‘s Algorithm
• A set T of edges is iteratively expanded to the
set of edges of the minimum spanning tree.
• T contains the edges of a forest and each tree
in this forest corresponds to a connected
component.
• In each step the edge with next smallest
weight is inserted into T to connect two trees.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
9
© Friedhelm Seutter, 2007
10
© Friedhelm Seutter, 2007
MST-Kruskal(G,w)
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
5
Disjoint Set Operations
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
11
© Friedhelm Seutter, 2007
12
© Friedhelm Seutter, 2007
MST-Example
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
6
MST-Example
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
13
© Friedhelm Seutter, 2007
14
© Friedhelm Seutter, 2007
MST-Example
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
7
Verification
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
15
© Friedhelm Seutter, 2007
Time-Complexity
• Initialization (lines 1-3):
O(|V |)
• Sorting the edges (line 4):
O(|E | log|E |)
• Passes of loop (lines 5-8):
• Disjoint set operations:
|E |
O(|E| lg*|E|))
• Worst-case running time:
O(|E | log|E |)
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
16
© Friedhelm Seutter, 2007
8
Storage Complexity
• Adjacency-List:
O( |V |+ |E | )
• Set T:
O( |E | )
• Disjoint Sets:
O( |V | )
• Worst-case storage needs: O( |V |+ |E | )
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
17
© Friedhelm Seutter, 2007
Shortest Paths
A motorist wishes to find the shortest possible
route from Brunswick to Berlin, Germany. Given
a road map of Germany on which the distance
between each pair of adjacent cities is marked,
how can we determine the shortest route?
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
18
© Friedhelm Seutter, 2007
9
Map of Germany
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
19
© Friedhelm Seutter, 2007
20
© Friedhelm Seutter, 2007
Weight of a Path
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
10
Shortest Path
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
21
© Friedhelm Seutter, 2007
Shortest-Paths Problems
• Single-source shortest-paths
(is solved by Dijkstra’s algorithm)
• Single-destination shortest-paths
• Single-pair shortest-path
• All-pairs shortest-paths
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
22
© Friedhelm Seutter, 2007
11
Predecessor Subgraph
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
23
© Friedhelm Seutter, 2007
Relaxation
• A technique called relaxation is denoting a
process of approximation.
• In Dijkstra’s algorithm the upper bounds for
shortest paths are tightened until they equal
the shortest path.
• The shortest-path estimates d[v] for vertices v
are initialized and then improved repeatedly.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
24
© Friedhelm Seutter, 2007
12
Initialize-Single-Source (G,s)
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
25
© Friedhelm Seutter, 2007
26
© Friedhelm Seutter, 2007
Relax (u,v,w)
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
13
Dijkstra‘s Algorithm
• A set of vertices S is maintained whose final
shortest paths weights from the source s have
already been determined.
• A vertex u ∈ V – S with the minimum shortestpath estimate is selected repeatedly, inserted
into S and all edges leaving u are relaxed.
The vertices in V – S are maintained in a
priority queue Q.
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
27
© Friedhelm Seutter, 2007
28
© Friedhelm Seutter, 2007
Dijkstra(G,w,s)
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
14
Dijkstra - Example
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
29
© Friedhelm Seutter, 2007
30
© Friedhelm Seutter, 2007
Dijkstra - Example
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
15
Dijkstra - Example
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
31
© Friedhelm Seutter, 2007
Shortest-Paths Tree
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
32
© Friedhelm Seutter, 2007
16
Verification
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
33
© Friedhelm Seutter, 2007
34
© Friedhelm Seutter, 2007
Verification
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
17
Time-Complexity
O(|V |)
• Initialization (lines 1-3):
• Passes of while-loop (lines 4-8):|V |
• Extraction of Q (line 5):
O(log|V |)
• Passes of for-loop (lines 7-8):
|E |
• Worst-case running time: O(|V | log|V | + |E |)
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
35
© Friedhelm Seutter, 2007
Storage Complexity
• Adjacency-List:
O( |V |+ |E | )
• Data structures
S, Q, d, π :
O( |V | )
• Worst-case storage needs: O( |V |+ |E | )
Fachhochschule Braunschweig / Wolfenbüttel
Institut für Angewandte Informatik
36
© Friedhelm Seutter, 2007
18

Documentos relacionados