Graphs Arrays Iteration Combining Data Structures Graphs The
Graphs Arrays Iteration Combining Data Structures
Graphs
The Scenario • Imagine we need to represent a highway system within our algorithm. Chattanooga Birmingham Atlanta Tampa Greenville
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.
Graphs Set of nodes and connecting edges.
Nodes and Edges B C D A E F G
Nodes and Edges B C D A E F G Nodes are also called vertices.
Nodes and Edges B C D A E G F Edges connect nodes.
Undirected Graphs B C D A E F G
Directed Graphs B C D A E F G Directed edges only allow movement in one direction.
Weighted Edges 7 B 5 12 C A 4 1 D 9 3 1 3 E 2 F 2 G Edge weights represent cost.
Directed Graphs Can be Weighted Too 7 B 5 12 C A 4 1 D 9 3 1 3 E 2 F 2 G
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) \
Representing Graphs How do we represent a node that has any number of children or connections?
What’s in a Node? Data + \ Edges (perhaps with weights)
A Low-level Diagram data children next_child this_child data children . .
LB 3 1 Another View Node 1 Children Node 2 Children Node 1 Node 3 Children Node 2 Node 4 Children Node 2 Node 3 2 4 Node 4 This represents a pointer to node 3 Node 3
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
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.
Questions?
Arrays
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.
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)
Terms • A cell or element represents one item in an array. • The index of a cell represents its location within the array.
Visually Representing Arrays A cell at the fourth index. 1 2 3 4 5 6 7 8 9 10
Defining Arrays LB Like a record definition, we define a new data type: Constant size Bounds/Range MAX is 10 Num. Array. Type definesa Array [1. . MAX] of Num Type name Cell type
LB Declaring an Array Variable Like declaring any other variable: My. Num. Array isoftype Num. Array. Type Variable name Type name
Accessing an Element in an Array Use brackets “[ ]” and specify an index value within the bounds: A number My. Num. Array[4] <- 42 An array 42 1 2 3 4 5 6 7 8 9 10
Multi-Dimension Arrays 1 2 3 4 5 2 -D 3 -D 1 2 3 4 5 6 7 8 9 10 1 2 3 4 -D & beyond can do, but visually ? ? ?
Defining A Two-Dimensional Array COLS is 10 ROWS is 5 Num. Array. Type definesa Array [1. . COLS] of Num 2 DNum. Array. Type definesa Array [1. . ROWS] of Num. Array. Type - or – 2 DNum. Array. Type definesa Array [1. . ROWS][1. . COLS] of Num
Accessing Elements in a 2 -D Array Row 1 2 3 4 5 My 2 DNum. Array 1 2 3 4 5 6 7 8 9 10 Column My 2 DNum. Array isoftype 2 DNum. Array. Type
Accessing Elements in a 2 -D Array Row 1 2 3 4 5 My 2 DNum. Array 1 2 3 4 5 6 7 8 9 10 Column My 2 DNum. Array
Accessing Elements in a 2 -D Array Row 1 2 3 4 5 My 2 DNum. Array 1 2 3 4 5 6 7 8 9 10 Column My 2 DNum. Array[3]
Accessing Elements in a 2 -D Array Row 1 2 3 4 5 My 2 DNum. Array 31 1 2 3 4 5 6 7 8 9 10 Column My 2 DNum. Array[3][8] <- 31 - or – My 2 DNum. Array[3, 8] <- 31 LB
Using Bounds Correctly 1 2 3 1 2 Row 3 4 5 Column 4 5 6 7 8 31 9 10 [3][8] . . 31 [8][3] (out of bounds)
Summary • Arrays – Are homogeneous collections – Are fixed in size – Are a linear data structure – Allow random, immediate access to elements
Questions?
Iteration
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.
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
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
Back to the Golf Course Go to the golf course Practice hitting balls on the driving range hole <- 1 loop Tee off Hit ball closer to hole until it goes in hole <- hole + 1 // move closer exitif (hole > 18) // terminating condition endloop Turn in scorecard to the pro shop
Not Always a Hole in One!. . . hole <- 1 loop Tee off loop exitif (ball in hole) hit ball closer to hole endloop hole <- hole + 1 exitif (hole > 18) endloop. . .
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.
The Loop Construct i, sum isoftype num sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) // prints 55
Tracing the Loop’s Behavior i 1 sum 0 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 1 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 2 sum 1 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 2 sum 1 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 2 sum 1 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 2 sum 3 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 3 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 3 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Time Passes. . .
Tracing the Loop’s Behavior i 3 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 3 sum 6 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 4 sum 6 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 4 sum 6 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 4 sum 6 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 4 sum 10 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 5 sum 10 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 5 sum 10 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 5 sum 10 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 5 sum 15 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 6 sum 15 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 6 sum 15 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 6 sum 15 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 6 sum 21 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 7 sum 21 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 7 sum 21 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 7 sum 21 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 7 sum 28 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 8 sum 28 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 8 sum 28 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 8 sum 28 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 8 sum 36 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 9 sum 36 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 9 sum 36 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 9 sum 36 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 9 sum 45 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 10 sum 45 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 10 sum 45 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 10 sum 45 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 10 sum 55 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 11 sum 55 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 11 sum 55 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 11 sum 55 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Tracing the Loop’s Behavior i 11 sum 55 sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)
Items to Consider with Loops • • Initialize values Determine exitif conditional and placement Perform work Increment counter (if needed)
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
Traversing the List. Head 96 48 34 17 • Iteratively repeat until we reach nil. 142 284 //
Start With a Framework loop ? ? ? exitif(? ? ? ) ? ? ? endloop
Initialize current isoftype ptr toa Node current <- List. Head // from elsewhere loop ? ? ? exitif(? ? ? ) ? ? ? endloop
Do the Work current isoftype ptr toa Node current <- List. Head // from elsewhere loop current^. data <- current^. data * 2 exitif(? ? ? ) ? ? ? endloop
Move Closer to the End current isoftype ptr toa Node current <- List. Head // from elsewhere loop current^. data <- current^. data * 2 current <- current^. next exitif(? ? ? ) ? ? ? endloop
When Can We Finish? current isoftype ptr toa Node current <- List. Head // from elsewhere loop current^. data <- current^. data * 2 current <- current^. next exitif(current = nil) ? ? ? endloop
But There’s a Problem! current isoftype ptr toa Node current <- List. Head // from elsewhere loop current^. data <- current^. data * 2 current <- current^. next exitif(current = nil) endloop
What If the List is Empty? current isoftype ptr toa Node current <- List. Head // from elsewhere loop exitif(current = nil) current^. data <- current^. data * 2 current <- current^. next endloop
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
LB A Simplified Teaching Example • • Imagine a classroom of 2 nd graders. We want to teach them multiplication. Jon’s wife’s process is: 1. Give them some instructions 2. Let them practice 3. Grade their work 4. Repeat steps 1 -3 until they know how to multiply
Start With a Framework loop ? ? ? exitif(? ? ? ) ? ? ? endloop
What Work Must We Do? loop teach class let students practice grade their work exitif(? ? ? ) ? ? ? endloop
When Can We Finish? loop teach class let students practice grade their work exitif(students know how to multiply) endloop
LB Test-Last Loops which have the exitif as the last line in the loop body. The loop executes at least once. Typically used when loop <statements> exitif(<conditional>) endloop This Is generated in here
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”
Start With a Framework loop ? ? ? exitif(? ? ? ) ? ? ? endloop
What Must We Do? choice isoftype string loop Print_Menu() read(choice) exitif(? ? ? ) ? ? ? endloop // displays menu // reads user’s choice
When Can We Finish? choice isoftype string loop Print_Menu() // displays menu read(choice) // reads user’s choice exitif(choice = “quit”) ? ? ? endloop
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
N-and-a-Half 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
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.
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 downhill endloop
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.
Questions?
Combining Data Structures
Basic Data Structures Some basic data structures: – Linked List – Binary Tree – Array But we can combine these as needed.
A Linked List of Arrays …
A Linked List of Arrays Defined MAX is 150 Num. Array definesa array [1. . MAX] of num Node definesa record data isoftype Num. Array next isoftype ptr toa Node endrecord
An Array of Linked Lists
An Array of Linked Lists Defined MAX is 150 Node definesa record data isoftype Char next isoftype ptr toa Node endrecord List. Array definesa array [1. . MAX] of ptr toa Node
Other Possibilities • A Binary Search Tree of Sorted Arrays of Unsorted Linked Lists • An Array of Linked Lists • A Linked List of Trees of Arrays • Etc.
Questions?
- Slides: 123