Title: Optimization Problems Minimum Spanning Tree Behavioral Abstraction
1Optimization Problems Minimum Spanning
TreeBehavioral Abstraction
Lecture 17
2Optimization Problems
3Optimization Algorithms
- Many real-world problems involve maximizing or
minimizing a value - How can a car manufacturer get the most parts out
of a piece of sheet metal? - How can a moving company fit the most furniture
into a truck of a certain size? - How can the phone company route calls to get the
best use of its lines and connections? - How can a university schedule its classes to make
the best use of classrooms without conflicts?
4Optimal vs. Approximate Solutions
- Often, we can make a choice
- Do we want the guaranteed optimal solution to the
problem, even though it might take a lot of
work/time to find? - Do we want to spend less time/work and find an
approximate solution (near optimal)?
5Approximate Solutions
6Greedy Algorithms
- Approximates optimal solution
- May or may not find optimal solution
- Provides quick and dirty estimates
- A greedy algorithm makes a series of
short-sighted decisions and does not look
ahead - Spends less time
7A Greedy Algorithm
- Consider the weight and value of some foreign
coins - foo 6.00 500 grams
- bar 4.00 450 grams
- baz 3.00 410 grams
- qux 0.50 300 grams
- If we can only fit 860 grams in our pockets...
- A greedy algorithm would choose
- 1 foo 500 grams 6.00
- 1 qux 300 grams 0.50
- Optimal solution is
- 1 bar 450 grams 4.00
- 1 baz 410 grams 3.00
Total of 6.50
Total of 7.00
8Short-Sighted Decisions
1
Start
2
End
12
1
9The Shortest Path Problem
- Given a directed, acyclic, weighted graph
- Start at some vertex A
- What is the shortest path from start vertex A to
some end vertex B?
10A Greedy Algorithm
2
7
5
Start
11
3
5
3
7
End
7
6
14
6
11A Greedy Algorithm
2
7
5
Start
11
3
5
3
7
End
7
6
14
6
12A Greedy Algorithm
2
7
5
Start
11
3
5
3
7
End
7
6
14
6
13A Greedy Algorithm
2
7
5
Start
11
3
5
3
7
End
7
6
14
6
Path 15
14A Greedy Algorithm
2
7
5
Start
11
3
5
3
7
End
7
6
14
6
Shortest Path 13
15Dynamic Planning
16Dynamic Planning
- Calculates all of the possible solution options,
then chooses the best one. - Implemented recursively.
- Produces an optimal solution.
- Spends more time.
17Bellmans Principle of Optimality
- Regardless of how you reach a particular state
(graph node), the optimal strategy for reaching
the goal state is always the same. - This greatly simplifies the strategy for
searching for an optimal solution.
18The Shortest Path Problem
- Given a directed, acyclic, weighted graph
- What is the shortest path from the start vertex
to some end vertex? - Minimize the sum of the edge weights
19Dynamic Planning
c
2
f
7
5
Start
11
b
3
e
5
3
7
d
a
End
7
6
14
6
g
20Dynamic Planning
f
Notation x means shortest path to x
7
b
e
5
End
6
b min(6g, 5e, 7f)
g
21Dynamic Planning
c
2
f
3
e
7
d
a
7
6
14
g min(6d, 14) e min(3c, 7d, 7g) f 2c
g
22Dynamic Planning
c
5
Start
11
3
d
a
c min(5, 11d) d 3
23b min(6g, 5e, 7f) g min(6d, 14) e
min(3c, 7d, 7g) f 2c c min(5, 11d) d 3
via a to d
24b min(6g, 5e, 7f) g min(6d, 14) e
min(3c, 7d, 7g) f 2c c min(5, 11d) d 3
via a to d
25b min(6g, 5e, 7f) g min(63, 14) e
min(3c, 73, 7g) f 2c c min(5, 113) d 3
via a to d
26b min(6g, 5e, 7f) g min(9, 14) e
min(3c, 10, 7g) f 2c c min(5, 14) d 3
via a to d
27b min(6g, 5e, 7f) g min(9, 14) e
min(3c, 10, 7g) f 2c c min(5, 14) d 3
via a to d
28b min(6g, 5e, 7f) g 9 via a to d to g e
min(3c, 10, 7g) f 2c c 5 via a to c d
3 via a to d
29b min(6g, 5e, 7f) g 9 via a to d to g e
min(3c, 10, 7g) f 2c c 5 via a to c d
3 via a to d
30b min(69, 5e, 7f) g 9 via a to d to g e
min(35, 10, 79) f 25 c 5 via a to c d
3 via a to d
31b min(15, 5e, 7f) g 9 via a to d to g e
min(8, 10, 16) f 7 via a to c to f c 5 via
a to c d 3 via a to d
32b min(15, 5e, 7f) g 9 via a to d to g e
min(8, 10, 16) f 7 via a to c to f c 5 via
a to c d 3 via a to d
33b min(15, 5e, 7f) g 9 via a to d to g e
8 via a to c to e f 7 via a to c to f c 5
via a to c d 3 via a to d
34b min(15, 5e, 7f) g 9 via a to d to g e
8 via a to c to e f 7 via a to c to f c 5
via a to c d 3 via a to d
35b min(15, 58, 77) g 9 via a to d to g e
8 via a to c to e f 7 via a to c to f c 5
via a to c d 3 via a to d
36b min(15, 13, 14) g 9 via a to d to g e 8
via a to c to e f 7 via a to c to f c 5
via a to c d 3 via a to d
37b min(15, 13, 14) g 9 via a to d to g e 8
via a to c to e f 7 via a to c to f c 5
via a to c d 3 via a to d
38b 13 via a to c to e to b g 9 via a to d
to g e 8 via a to c to e f 7 via a to c
to f c 5 via a to c d 3 via a to d
39Dynamic Planning
c
2
f
7
5
Start
11
b
3
e
5
3
7
d
a
End
7
6
14
6
Shortest Path 13
g
40Summary
- Greedy algorithms
- Make short-sighted, best guess decisions
- Required less time/work
- Provide approximate solutions
- Dynamic planning
- Examines all possible solutions
- Requires more time/work
- Guarantees optimal solution
41Questions?
42Minimum Spanning Tree
- Prims AlgorithmKruskals Algorithm
43The Scenario
- Construct a telephone network
- Weve got to connect many cities together
- Each city must be connected
- We want to minimize the total cable used
44The Scenario
- Construct a telephone network
- Weve got to connect many cities together
- Each city must be connected
- We want to minimize the total cable used
45Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
46Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
1
3
2
47Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
1
1
3
3
2
2
48Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
1
1
3
3
2
2
S.T. 3
S.T. 5
S.T. 4
49Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
1
1
1
3
3
3
2
2
2
M.S.T. 3
S.T. 5
S.T. 4
50Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
- Can start at any node
51Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
- Can start at any node
- Unique solution.
- Can come from different sets of edges
4
4
4
52Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
- Can start at any node
- Unique solution.
- Can come from different sets of edges
4
4
Pick any 2
4
53Minimum Spanning Tree
- Required Connect all nodes at minimum cost.
- Cost is sum of edge weights
- Can start at any node
- Unique solution.
- Can come from different sets of edges
- Two algorithms
- Prims
- Kruskals
54Prims Algorithm
- Start with any vertex.
- All its edges are candidates.
- Stop looking when all vertices are picked
- Otherwise repeat
- Pick minimum edge (no cycles) and connect the
adjacent vertex - Add all edges from that new vertex to our
candidates
55Prims Algorithm
- Pick any vertex and add it to vertices list
- Loop
- Exitif (vertices contains all the vertices
in graph) - Select shortest, unmarked edge coming from
vertices list - If (shortest edge does NOT create a cycle)
then - Add that edge to your edges
- Add the adjoining vertex to the vertices list
- Endif
- Mark that edge as having been considered
- Endloop
564
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
57Start
4
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
584
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
594
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
604
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
614
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
624
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
634
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
644
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
NO!
9
17
19
11
654
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
LOOP
9
17
19
11
664
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
674
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
684
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
694
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
704
5
Prims MST 66
3
9
2
7
6
10
9
11
71An Alternate AlgorithmKruskals Algorithm
72Kruskals Algorithm
- Sort edges in graph in increasing order
- Select the shortest edge
- Loop
- Exitif all edges examined
- Select next shortest edge (if no cycle created)
- Mark this edge as examined
- Endloop
- This guarantees an MST, but as it is built, edges
do not have to be connected
734
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
74Sort the Edges
- 2, 3, 4, 5, 6, 7, 8, 9, 9, 10, 11, 12, 13, 14,
15, 17, 18, 19, 20, 21, 24
754
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
764
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
774
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
784
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
794
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
804
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
814
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
Still NO!
9
17
19
11
824
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
83Or
844
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
854
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
864
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
874
5
3
9
18
2
7
14
15
12
21
24
13
20
8
6
10
9
17
19
11
884
5
3
9
2
7
Kruskals MST 66
6
10
9
11
894
5
Prims MST 66
3
9
2
7
6
10
9
11
90Summary
- Minimum spanning trees
- Connect all vertices
- With no cycles
- And minimize the total edge cost
- Prims and Kruskals algorithm
- Generate minimum spanning trees
- Give same total cost, but may give different
trees (if graph has edges with same weight)
91Questions?
92Introduction to theObject-Oriented Paradigm
93The Scenario
- Recall the concept of a Queue
- Defined by a set of behaviors
- Enqueue to the end
- Dequeue from the front
- First in, first out
Items
94Where is the Queue?
- The logical idea of the queue consisted of
- Data stored in some structure (list, array, etc.)
- Modules to act on that data
- But where is the queue?
- Wed like some way of representing sucha
structure in our programs.
95Issues
- As is, there is no way to protect against
violating the specified behaviors.
Enqueue
Dequeue
Sneak into Middle
Items
96Issues
- Wed like a way to put a wrapper around our
structure to protect against this.
Enqueue
Dequeue
Sneak into Middle
Items
97Motivation
- We need ways to manage the growing complexity and
size of programs. - We can better model our algorithmic solutions to
real world phenomenon. - Contracts of responsibility can establish clearer
communication and more protected manipulation.
98Procedural Abstraction
- Procedural Abstractions organize instructions.
Function Power Give me two numbers (base
exponent) Ill return to you baseexponent ???
Implementation ???
99Data Abstraction
- Data Abstractions organize data.
StudentType
Name (string) GPA (num) Letter Grade
(char) Student Number (num)
100Behavioral Abstraction
- Behavioral Abstractions combine procedural and
data abstractions.
Queue Object
Enqueue
Is Full
Data State
Is Empty
Dequeue
Initialize
101The Object-Oriented Paradigm
- Instances of behavioral abstractions are known as
objects. - Objects have a clear interface by which they send
and receive messages (communicate). - OO is a design and approach issue. Just because
a language offers object-oriented features
doesnt mean youre doing OO programming.
102Benefits of Object-Oriented Paradigm
- Encapsulation information hiding, control, and
design - Reuse create libraries of tested, optimized
classes - Adaptability controlled interactions with
minimal coupling - Better model real world and larger problems
- Break down tasks better
103Information Hiding
- Information Hiding means that the user has enough
information to use the interface, and no more
- Example stick shift
- We dont care about the inner workings
- We just want to get the car going!
1
3
5
2
4
R
104Encapsulation
- Encapsulation is the grouping of related things
together within some structure
Item 1
Item 2
Item3
105Encapsulation via Algorithms
- Algorithms encapsulate modules, data, and
instructions.
Algorithm
Procedure
Instructions
Function
Data
106Encapsulating Instructions
- Modules encapsulate instructions.
Procedure/Function
Instructions
Instructions
Module call
Instructions
107Encapsulating Data
- Records allow us to do this with data
Record
Field 1
Field 2
Record
108Revisiting the Where is it? Question
- Notice we still have no way of identifying the
idea were discussing - The Queue is still in the ether.
- Wed like to encapsulate the data (regardless of
its actual representation) with the behavior
(modules). - Once we do this, weve got a logical entity to
which we can point and say, there it is!
109Encapsulating Data with Methods
- Abstract data types (ADTs) allow us to do this
with logically related data and modules
Queue
Enqueue
Data
Dequeue
110Abstract Data Types
- An idea, a concept, an abstraction of the big
picture - It encapsulates the abstract behaviors and data
implied by the thing being modeled.
Queue
Enqueue
Is Full
Data State
Is Empty
Dequeue
Initialize
111Achieving Behavioral Abstraction
- Abstract data types (ADTs) are concepts.
- We require some way to implement these common
abstractions so we can write them once, verify
that they are correct, and reuse them. - This would save us from having to re-do work.
For example, every time we create a queue we did - List_Node definesa ...q_front isoftype
...q_tail isoftype ... - procedure Enqueue(...)
- procedure Dequeue(...)
- We need an algorithmic construct that will allow
us to bundle these things together the class.
112Summary
- Behavioral abstraction combines data abstraction
with procedural abstraction. - The object-oriented paradigm focuses on the
interaction and manipulation of objects. - An Abstract Data Type (ADT) allows us to think of
what were representing as a thing regardless of
its actual implementation.
113Questions?
114(No Transcript)