Graphs Arrays Iteration Combining Data Structures
description
Transcript of Graphs Arrays Iteration Combining Data Structures
![Page 1: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/1.jpg)
GraphsArrays
IterationCombining Data Structures
![Page 2: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/2.jpg)
Graphs
![Page 3: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/3.jpg)
The Scenario• Imagine we need to represent a highway
system within our algorithm.
Atlanta
Chattanooga
Tampa
Birmingham
Greenville
![Page 4: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/4.jpg)
Which Data Structure?
• Arrays are fixed in size and linear• Lists are dynamic and linear• Trees are dynamic and hierarchical
• We need a non-linear structure which connects nodes to other nodes.
![Page 5: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/5.jpg)
Graphs
Set of nodes and connecting edges.
![Page 6: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/6.jpg)
Nodes and Edges
B
F
CA
EG
D
![Page 7: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/7.jpg)
Nodes and Edges
B
F
CA
EG
D
Nodes are also called vertices.
![Page 8: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/8.jpg)
Nodes and Edges
B
F
CA
EG
D
Edges connect nodes.
![Page 9: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/9.jpg)
Undirected Graphs
B
F
CA
EG
D
![Page 10: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/10.jpg)
Directed Graphs
B
F
CA
EG
D
Directed edges only allow movement in one direction.
![Page 11: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/11.jpg)
Weighted Edges
B
F
CA
EG
D5
3
2
1
4
7
1
2
39
12
Edge weights represent cost.
![Page 12: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/12.jpg)
Directed Graphs Can be Weighted Too
B
F
CA
EG
D5
3
2
1
4
7
1
2
39
12
![Page 13: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/13.jpg)
Trees and Lists are Graphs
• Trees and lists are examples of graphs• We’ll use the term graph for situations– that don’t have the implied restrictions– i.e. non hierarchical (many-to-many)
\\
![Page 14: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/14.jpg)
Representing Graphs
How do we represent a node that has any number of children or connections?
![Page 15: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/15.jpg)
What’s in a Node?
Data
+\\
Edges (perhaps with weights)
![Page 16: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/16.jpg)
A Low-level Diagram
childrendata
childrendata
childrendata
childrendata
next_childthis_child
next_childthis_child
next_childthis_child
... ... ...
![Page 17: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/17.jpg)
Another View
Node 1Children
Node 2Children
Node 3Children
Node 4Children
Node 2 Node 3 Node 4
Node 1
Node 2
Node 2 Node 3
1
24
3
This representsa pointer to node 3
LB
![Page 18: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/18.jpg)
Representing Non-binary Trees and
Graphs Tree_Node definesa Record data isoftype String children isoftype Ptr toa Child_List_Node endrecord //Tree_Node
Child_List_Node definesa Record this_child isoftype Ptr toa Tree_Node next_child isoftype Ptr toa Child_List_Node endrecord //Child_List_Node
![Page 19: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/19.jpg)
Summary
• Graphs are a collection of edges and nodes– Non-hierarchical and non-linear– Edges can be weighted or unweighted– Edges can be directed or undirected
• Graphs allow a “many to many” relationship between nodes.
![Page 20: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/20.jpg)
Questions?
![Page 21: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/21.jpg)
Arrays
![Page 22: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/22.jpg)
The Scenario
• We need a data structure to hold information.
• We know ahead of time how many items we need to hold.
• All of the items are of the same type.• We need fast access to each element
in the collection.
![Page 23: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/23.jpg)
Properties of Arrays
• Linear data structure• Homogeneous collection– All entries are of the same type
• Static and cannot grow or shrink• Allow random access– Like a CD player (vs. a tape player)
![Page 24: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/24.jpg)
Terms
• A cell or element represents one item in an array.
• The index of a cell represents its location within the array.
![Page 25: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/25.jpg)
Visually Representing Arrays
A cell at the fourth index.
1 2 3 4 5 6 7 8 9 10
![Page 26: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/26.jpg)
Defining Arrays
Like a record definition, we define a new data type:
MAX is 10NumArrayType definesa Array [1..MAX] of Num
Constant size
Type name
Bounds/Range
Cell type
LB
![Page 27: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/27.jpg)
Declaring an Array Variable
Like declaring any other variable:
MyNumArray isoftype NumArrayType
Type nameVariable name
LB
![Page 28: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/28.jpg)
Accessing an Element in an Array
Use brackets “[ ]” and specify an index value within the bounds:
MyNumArray[4] <- 42An array
A number
42
1 2 3 4 5 6 7 8 9 10
![Page 29: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/29.jpg)
Multi-Dimension Arrays
2-D
3-D
1 2 3 4 5 6
12345 1
23
12345
1 2 3 4 5 6 7 8 9 10
4-D & beyondcan do,
but visually ???
![Page 30: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/30.jpg)
Defining A Two-Dimensional Array
COLS is 10ROWS is 5
NumArrayType definesa Array [1..COLS] of Num2DNumArrayType definesa Array [1..ROWS] of
NumArrayType
- or –
2DNumArrayType definesa Array [1..ROWS][1..COLS] of Num
![Page 31: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/31.jpg)
Accessing Elements in a 2-D Array
Row
Column
12345
1 2 3 4 5 6 7 8 9 10
My2DNumArray
My2DNumArray isoftype 2DNumArrayType
![Page 32: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/32.jpg)
Accessing Elements in a 2-D Array
Row
Column
12345
1 2 3 4 5 6 7 8 9 10
My2DNumArray
My2DNumArray
![Page 33: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/33.jpg)
Accessing Elements in a 2-D Array
Row
Column
12345
1 2 3 4 5 6 7 8 9 10
My2DNumArray
My2DNumArray[3]
![Page 34: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/34.jpg)
Accessing Elements in a 2-D Array
Row
Column
31
12345
1 2 3 4 5 6 7 8 9 10
My2DNumArray
My2DNumArray[3][8] <- 31 - or –My2DNumArray[3,8] <- 31
LB
![Page 35: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/35.jpg)
Using Bounds Correctly
31
31
.
.
[3][8]
[8][3] (out of bounds)
1 2 3 4 5 6 7 8 9 10
12345
Row
Column
![Page 36: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/36.jpg)
Summary
• Arrays– Are homogeneous collections– Are fixed in size– Are a linear data structure– Allow random, immediate access to
elements
![Page 37: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/37.jpg)
Questions?
![Page 38: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/38.jpg)
Iteration
![Page 39: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/39.jpg)
An Example: Golf
1. Go to the golf course.2. Practice hitting balls on the driving range.3. Go to the first hole.4. Tee off.5. Hit ball closer to hole until it goes in.6. Move to next hole.7. If you haven’t played all 18 holes, then repeat
steps 4-7.8. Turn in scorecard to the pro shop.
![Page 40: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/40.jpg)
The Scenario
• We need a way to repeat instructions• Recursion allows this via module calls,• But what about another solution…
• We’ll use iteration to achieve repetition– Need some way of marking which
instructions to repeat– Need some way to determine when to
stop repeating
![Page 41: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/41.jpg)
Three Properties of Repetition
• Need some way of repeating (or starting the instructions again)
• Need to know when to stop repeating (when finished)
• Need to do some work and move closer to being finished
![Page 42: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/42.jpg)
Back to the Golf Course
Go to the golf coursePractice hitting balls on the driving range
hole <- 1loop Tee off Hit ball closer to hole until it goes in hole <- hole + 1 // move closer exitif (hole > 18) // terminating conditionendloop
Turn in scorecard to the pro shop
![Page 43: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/43.jpg)
Not Always a Hole in One!...hole <- 1loop Tee off loop exitif (ball in hole) hit ball closer to hole endloop hole <- hole + 1 exitif (hole > 18)endloop...
![Page 44: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/44.jpg)
Iteration
Allows for the repetition of instructions.
• loop begins the iteration.• exitif(<conditional expression>)
provides a terminating condition; when the conditional expression is true, then execution jumps to the algorithm step after endloop and continues.
• endloop ends the iteration section.
![Page 45: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/45.jpg)
The Loop Constructi, sum isoftype num
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum) // prints 55
![Page 46: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/46.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
1 0
![Page 47: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/47.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
1 1
![Page 48: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/48.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
2 1
![Page 49: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/49.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
2 1
![Page 50: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/50.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
2 1
![Page 51: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/51.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
2 3
![Page 52: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/52.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
3 3
![Page 53: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/53.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
3 3
![Page 54: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/54.jpg)
Time Passes...
![Page 55: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/55.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
3 3
![Page 56: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/56.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
3 6
![Page 57: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/57.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
4 6
![Page 58: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/58.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
4 6
![Page 59: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/59.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
4 6
![Page 60: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/60.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
4 10
![Page 61: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/61.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
5 10
![Page 62: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/62.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
5 10
![Page 63: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/63.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
5 10
![Page 64: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/64.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
5 15
![Page 65: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/65.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
6 15
![Page 66: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/66.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
6 15
![Page 67: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/67.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
6 15
![Page 68: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/68.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
6 21
![Page 69: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/69.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
7 21
![Page 70: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/70.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
7 21
![Page 71: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/71.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
7 21
![Page 72: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/72.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
7 28
![Page 73: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/73.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
8 28
![Page 74: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/74.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
8 28
![Page 75: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/75.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
8 28
![Page 76: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/76.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
8 36
![Page 77: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/77.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
9 36
![Page 78: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/78.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
9 36
![Page 79: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/79.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
9 36
![Page 80: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/80.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
9 45
![Page 81: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/81.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
10 45
![Page 82: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/82.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
10 45
![Page 83: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/83.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
10 45
![Page 84: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/84.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
10 55
![Page 85: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/85.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
11 55
![Page 86: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/86.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
11 55
![Page 87: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/87.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
11 55
![Page 88: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/88.jpg)
Tracing the Loop’s Behavior
i sum
sum <- 0 // initializei <- 1 // initializeloop exitif (i > 10) sum <- sum + i // work i <- i + 1 // incrementendloopprint(sum)
11 55
![Page 89: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/89.jpg)
Items to Consider with Loops
• Initialize values• Determine exitif conditional and
placement• Perform work• Increment counter (if needed)
![Page 90: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/90.jpg)
An Iterative List Traversal Example
• Given some linked list of numbers, double each element in the list
• We’ll do this iteratively:– Work to be done is the doubling of the
elements– Stop when we reach the end of the list
![Page 91: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/91.jpg)
Traversing the List
• Iteratively repeat until we reach nil.
48 17 142ListHead //96 34 284
![Page 92: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/92.jpg)
Start With a Framework
loop ??? exitif(???) ???endloop
![Page 93: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/93.jpg)
Initialize
current isoftype ptr toa Nodecurrent <- ListHead // from elsewhere
loop ??? exitif(???) ???endloop
![Page 94: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/94.jpg)
Do the Work
current isoftype ptr toa Nodecurrent <- ListHead // from elsewhere
loop current^.data <- current^.data * 2 exitif(???) ???endloop
![Page 95: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/95.jpg)
Move Closer to the End
current isoftype ptr toa Nodecurrent <- ListHead // from elsewhere
loop current^.data <- current^.data * 2 current <- current^.next exitif(???) ???endloop
![Page 96: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/96.jpg)
When Can We Finish?
current isoftype ptr toa Nodecurrent <- ListHead // from elsewhere
loop current^.data <- current^.data * 2 current <- current^.next exitif(current = nil) ???endloop
![Page 97: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/97.jpg)
But There’s a Problem!
current isoftype ptr toa Nodecurrent <- ListHead // from elsewhere
loop current^.data <- current^.data * 2 current <- current^.next exitif(current = nil)endloop
![Page 98: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/98.jpg)
What If the List is Empty?
current isoftype ptr toa Nodecurrent <- ListHead // from elsewhere
loop exitif(current = nil) current^.data <- current^.data * 2 current <- current^.nextendloop
![Page 99: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/99.jpg)
Sentinel Loops
A “sentinel” is a guard, so a sentinel loop is one in which the loop is “guarded.”
Loops which have the exitif as the first line in the loop body.
The loop may execute 0 or more times.
loop exitif(<conditional>) <statements>endloop
![Page 100: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/100.jpg)
A Simplified Teaching Example
• Imagine a classroom of 2nd graders.• We want to teach them multiplication.
Jon’s wife’s process is:
1. Give them some instructions2. Let them practice3. Grade their work4. Repeat steps 1-3 until they know how to
multiply
LB
![Page 101: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/101.jpg)
Start With a Framework
loop ??? exitif(???) ???endloop
![Page 102: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/102.jpg)
What Work Must We Do?
loop teach class let students practice grade their work exitif(???) ???endloop
![Page 103: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/103.jpg)
When Can We Finish?
loop teach class let students practice grade their work exitif(students know how to multiply)endloop
![Page 104: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/104.jpg)
Test-Last Loops
Loops which have the exitif as the last line in the loop body.
The loop executes at least once.
loop <statements> exitif(<conditional>)endloop
Typically used when
This
Is generatedin here
LB
![Page 105: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/105.jpg)
A Sample Problem
Design a system that• Presents a menu to the user• Reads in the user’s choice• Processes the user’s choice• Until the user types “quit”
![Page 106: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/106.jpg)
Start With a Framework
loop ??? exitif(???) ???endloop
![Page 107: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/107.jpg)
What Must We Do?
choice isoftype string
loop Print_Menu() // displays menu read(choice) // reads user’s choice exitif(???) ???endloop
![Page 108: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/108.jpg)
When Can We Finish?
choice isoftype string
loop Print_Menu() // displays menu read(choice) // reads user’s choice exitif(choice = “quit”) ???endloop
![Page 109: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/109.jpg)
What Else – Do Work!
choice isoftype string
loop Print_Menu() // displays menu read(choice) // reads user’s choice exitif(choice = “quit”) Process_Choice(choice)endloop
![Page 110: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/110.jpg)
N-and-a-Half Loops
Loops which have the exitif in the middle of other instructions in the loop body.
The “before statements” will execute one more time than the “after statements”.
loop <before statements> exitif(<conditional>) <after statements>endloop
![Page 111: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/111.jpg)
Placement of the Exitif Statement
• The exitif conditional statement can be placed anywhere in the loop:– At the beginning, before instructions– At the end, after all instructions– In the middle of instructions
• Often, changing the placement of the exitif conditional alters the number of iterations performed.
![Page 112: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/112.jpg)
Sisyphus’ Infinite Loop
Make sure that your exitif conditions are correctly placed and will be true!
loop select a rock roll the rock uphill exitif(no more rocks below) go back downhillendloop
![Page 113: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/113.jpg)
Summary
• Iteration allows us to repeat instructions until some exit criteria is met.
• We have choice in the placement of the exit condition:– At the front (sentinel)– At the end (test last)– In the middle (N-and-a-half)
• Be sure to trace your loops and test to see they exit correctly.
![Page 114: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/114.jpg)
Questions?
![Page 115: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/115.jpg)
Combining Data Structures
![Page 116: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/116.jpg)
Basic Data Structures
Some basic data structures:– Linked List– Binary Tree– Array
But we can combine these as needed.
![Page 117: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/117.jpg)
A Linked List of Arrays
…
![Page 118: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/118.jpg)
A Linked List of Arrays Defined
MAX is 150
NumArray definesa array [1..MAX] of num
Node definesa record data isoftype NumArray next isoftype ptr toa Nodeendrecord
![Page 119: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/119.jpg)
An Array of Linked Lists
![Page 120: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/120.jpg)
An Array of Linked Lists Defined
MAX is 150
Node definesa record data isoftype Char next isoftype ptr toa Nodeendrecord ListArray definesa array [1..MAX] of ptr toa Node
![Page 121: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/121.jpg)
Other Possibilities
• A Binary Search Tree of Sorted Arrays of Unsorted Linked Lists
• An Array of Linked Lists of Linked Lists• A Linked List of Trees of Arrays• Etc.
![Page 122: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/122.jpg)
Questions?
![Page 123: Graphs Arrays Iteration Combining Data Structures](https://reader035.fdocuments.net/reader035/viewer/2022081507/56815cfe550346895dcafc85/html5/thumbnails/123.jpg)