1.23k likes | 1.4k Views
Lecture 13. Graphs Arrays Iteration Combining Data Structures. Lecture 13. Graphs. The Scenario. Imagine we need to represent a highway system within our algorithm. Chattanooga. Greenville. Atlanta. Birmingham. Tampa. Which Data Structure?. Arrays are fixed in size and linear
E N D
Lecture 13 GraphsArraysIterationCombining Data Structures
Lecture 13 Graphs
The Scenario • Imagine we need to represent a highway system within our algorithm. Chattanooga Greenville Atlanta Birmingham Tampa
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 G F
Nodes and Edges B C D A E G F 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 G F
Directed Graphs B C D A E G F Directed edges only allow movement in one direction.
Weighted Edges B 7 5 C 12 D A 4 1 9 3 1 E 3 G 2 F 2 Edge weights represent cost.
Directed Graphs Can be Weighted Too B 7 5 C 12 D A 4 1 9 3 1 E 3 G 2 F 2
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)
data data data data children children children children next_child next_child next_child this_child this_child this_child ... ... ... A Low-level Diagram
Node 3 Node 2 Node 2 Node 1 Node 4 Node 3 Node 4 Node 3 Node 2 Node 1 Node 2 Children Children Children Children This represents a pointer to node 3 LB 3 1 Another View 2 4
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.
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: MAX is 10 NumArrayType definesa Array [1..MAX] of Num Constant size Bounds/Range Cell type Type name
LB Declaring an Array Variable Like declaring any other variable: MyNumArray isoftype NumArrayType Variable name Type name
Accessing an Element in an Array Use brackets “[ ]” and specify an index value within the bounds: MyNumArray[4] <- 42 A number 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 5 4-D & beyond can do, but visually??? 3 2 1 1 2 3 4 5 6
Defining A Two-Dimensional Array COLS is 10 ROWS is 5 NumArrayType definesa Array [1..COLS] of Num 2DNumArrayType definesa Array [1..ROWS] of NumArrayType - or – 2DNumArrayType definesa Array [1..ROWS][1..COLS] of Num
Accessing Elements in a 2-D Array Row 1 2 3 4 5 My2DNumArray 1 2 3 4 5 6 7 8 9 10 Column My2DNumArray isoftype 2DNumArrayType
Accessing Elements in a 2-D Array Row 1 2 3 4 5 My2DNumArray 1 2 3 4 5 6 7 8 9 10 Column My2DNumArray
Accessing Elements in a 2-D Array Row 1 2 3 4 5 My2DNumArray 1 2 3 4 5 6 7 8 9 10 Column My2DNumArray[3]
Accessing Elements in a 2-D Array Row 1 2 3 4 5 My2DNumArray 31 1 2 3 4 5 6 7 8 9 10 Column My2DNumArray[3][8] <- 31 - or – My2DNumArray[3,8] <- 31 LB
Using Bounds Correctly Column 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 Row [3][8] 31 . . [8][3] (out of bounds) 31
Summary • Arrays • Are homogeneous collections • Are fixed in size • Are a linear data structure • Allow random, immediate access to elements
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 1 0 i sum 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 1 1 i sum 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 2 1 i sum 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 2 1 i sum 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 2 1 i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum)