Upload
stacy
View
6
Download
1
Tags:
Embed Size (px)
DESCRIPTION
alg pro solvd
Citation preview
Lectures((clicker(use(
Lecture'par*cipa*on:' Purchase(your(clicker( Register(your(clicker(ID(on(the(FIT1029(Moodle(site(
Label(your(clicker((so(you(dont(lose(or(swap(it)(
1(
FIT 1029 Algorithmic Problem Solving
Lecture 2 Fundamental Data Structures
Prepared by Julian Garca
COMMONWEALTH OF AUSTRALIA Copyright Regulations 1969
WARNING This material has been reproduced and communicated to you by or on behalf of Monash University pursuant to Part VB of the Copyright Act 1968 (the Act).The material in this communication may be subject to copyright under the Act.Any further
reproduction or communication of this material by you may be the subject of copyright protection under the Act. Do not remove this notice.
Lists
GraphsFirst Item.Second Item.
Third Item.
Tables
List
List
Simple List
First itemAnother itemYet another item
0.1.2.
ItemsIndices
NameStart numbering at 0.
Notation
0. stop1. pots2. tops
Anagrams
Anagrams[1] = pots
Anagrams = [stop, pots, tops]
Referring to elements in a list
L 1 2 3 4 5 6 70 1 2 3 4 5 6
L[0, 6] or L
L 1 2 3 4 5 6 70 1 2 3 4 5 6
L[0, 2]
L 1 2 3 4 5 6 70 1 2 3 4 5 6
L[2, 5]
L[0, n-1]A list L of size n
L 1 2 3 4 5 6 70 1 2 3 4 5 6
L[1] is 2
Using lists to solve a problem
Age of the kidsTwo men meet on the street A: All three of my sons celebrate their birthday today. Can you tell
me how old each one is? B: Yes, but you have to tell me something about them A: The product of their ages is 40. B: I need more info A: The sum of their ages is equal to the number of windows in the
building next to us B: I need more info A: My youngest son has blue eyes. B: That is sufficient!
Age of the kidsTwo men meet on the street A: All three of my sons celebrate their birthday today. Can you
tell me how old each one is? B: Yes, but you have to tell me something about them A: The product of their ages is 40. B: I need more info A: The sum of their ages is equal to the number of windows in the
building next to us B: I need more info A: My youngest son has blue eyes. B: That is sufficient!
2 Z+Y,M,E
1 Y M E
Y M E = 40
40
All three of my sons celebrate their birthday today. Can you tell me how old each one is?
The product of their ages is 40.
Possible solutions
1, 1, 400.1. 1, 2, 202. 1, 4, 10
1, 5, 83.2, 2, 104.2, 4, 55.
Have we got all possible solutions? check!
Age of the kidsTwo men meet on the street A: All three of my sons celebrate their birthday today. Can you
tell me how old each one is? B: Yes, but you have to tell me something about them A: The product of their ages is 40. B: I need more info A: The sum of their ages is equal to the number of windows in
the building next to us B: I need more info A: My youngest son has blue eyes. B: That is sufficient!
Simple List
1, 1, 400.1.2.
1, 2, 201, 4, 101, 5, 82, 2, 102, 4, 5
3.4.5.
422315141411
Age of the kidsTwo men meet on the street A: All three of my sons celebrate their birthday today. Can you
tell me how old each one is? B: Yes, but you have to tell me something about them A: The product of their ages is 40. B: I need more info A: The sum of their ages is equal to the number of windows in
the building next to us B: I need more info A: My youngest son has blue eyes. B: That is sufficient!
Simple List
1, 1, 400.1.2.
1, 2, 201, 4, 101, 5, 82, 2, 102, 4, 5
3.4.5.
422315141411
Age of the kidsTwo men meet on the street A: All three of my sons celebrate their birthday today. Can you
tell me how old each one is? B: Yes, but you have to tell me something about them A: The product of their ages is 40. B: I need more info A: The sum of their ages is equal to the number of windows in
the building next to us B: I need more info A: My youngest son has blue eyes. B: That is sufficient!
Simple List
1, 1, 400.1.2.
1, 2, 201, 4, 101, 5, 82, 2, 102, 4, 5
3.4.5.
422315141411
y = 1, m =5, e = 8
Age of the kidsConstruct a list of possible solutions
For each possible solution, find the sum of the ages
Find those solutions which have the same sum of ages
Find out of those solutions which ones have an youngest son
Start
End
Lists
GraphsFirst Item.Second Item.
Third Item.
Tables
Binomial[4, 2] = 6
0 1 2 3 4
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
Binomial:
Name of Table
Row Column
Column numbers
Row numbers
Tables
Distance[2,1] = 12
3 2 14 5
1 1 4 2
1 12 13 2
11 3 3 1
Distance:0 1 2 3
0
1
2
3
M[0..n-1 , 0.. m-1], table M with n rows and m columns.
Distance = [[3, 2, 14, 5], [1,1,4,2], [1,12,13,2], [11,3,3,1]]
Graphs
Source: http://www.facebook.com/notes/facebook-engineering/visualizing-friendships/469716398919
Visualizing friendship, Graph by Paul Butler.
Creating MazesDevelop a procedure for creating mazes.
Nine crossroads Andrea Gilbert (www.clickmazes.com)
First how do we represent a maze?
Simple Maze
Enter
Exit
l l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l lEnter
Exit
Mark the junctions
Make passage ways
l l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l lEnter
Exit
Representation of Maze
l l
l l l
l
l
l l
l l
l
l
l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l
l
Graphs
VertexVertex
Vertex
GraphsEdge
Edge
Edge
Graphs
Path
Start vertex
Endvertex
Graphs
Circuit
Circuit
No repetition of edges allowed
Undirected vs Directed
Non-simple vs Simple
Non-simple vs SimpleNo Multiple
edges
No Loops
Non-simple vs Simple
Disconnected vs Connected
Disconnected vs Connected
There is a path between any two vertices
Unweighted/Weighted2
16
12
10
1
0 2
43
5
24
Unlabelled/Labelled
Q
W
CBA
X
TE
P
Trees
Trees
Graphs which are: Simple
no loops or multiple edges
Connected No circuits.
Spanning Tree of a graph G
Spanning Tree of a graph G
Spanning Tree of a graph G
Assume G is simple and connected.
A spanning tree of G is a tree which:
Contains all the vertices of G, and
The edges of the tree are edges of G.
How to get a spanning tree?
Prims algorithmChoose a vertex in the
Graph
Put the vertex in the Tree
Is there a vertex not in the Tree?
Choose an edge between the Tree and this vertex
Add the edge to the Tree.
Yes
Start
EndNo
OutputInput Is the output a tree? Contains all vertices in input?
Edges are edges of the input?
Can you envision any real world applications of Prims algorithm?
Start
End
Start
End
Iteration
Selection
Conditions
Fixed iteration vs Conditional Iteration
What about mazes?
Start with a Grid
l l
l l l
l
l
l l
l l
l
l
l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
Find a Spanning Tree
l l
l l l
l
l
l l
l l
l
l
l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
Find a Spanning Tree
l l
l l l
l
l
l l
l l
l
l
l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
Add entrance and exit
l l
l l l
l
l
l l
l l
l
l
l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l
l
Enter
Exit
Put in walls
l l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l
l l l l l l l lEnter
Exit
Remove Tree
Enter
Exit
Summary
Lists Tables Graphs
Water Bottle ProblemSuppose you have a 5 litre bottle and a 3 litre bottle (without markings). How can you get
exactly 4 litres from the well?
3 5
Before Next LectureOn moodle: Watch the following videos:
What is an Algorithm? The components of a complex algorithm?
Read Describing Algorithms, by Robyn A. McNamara (again).