1 / 123

Graphs Arrays Iteration Combining Data Structures

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

lynnea
Download Presentation

Graphs Arrays Iteration Combining Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 13 GraphsArraysIterationCombining Data Structures

  2. Lecture 13 Graphs

  3. The Scenario • Imagine we need to represent a highway system within our algorithm. Chattanooga Greenville Atlanta Birmingham Tampa

  4. 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.

  5. Graphs Set of nodes and connecting edges.

  6. Nodes and Edges B C D A E G F

  7. Nodes and Edges B C D A E G F Nodes are also called vertices.

  8. Nodes and Edges B C D A E G F Edges connect nodes.

  9. Undirected Graphs B C D A E G F

  10. Directed Graphs B C D A E G F Directed edges only allow movement in one direction.

  11. Weighted Edges B 7 5 C 12 D A 4 1 9 3 1 E 3 G 2 F 2 Edge weights represent cost.

  12. Directed Graphs Can be Weighted Too B 7 5 C 12 D A 4 1 9 3 1 E 3 G 2 F 2

  13. 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) \\

  14. Representing Graphs How do we represent a node that has any number of children or connections?

  15. What’s in a Node? Data + \\ Edges (perhaps with weights)

  16. data data data data children children children children next_child next_child next_child this_child this_child this_child ... ... ... A Low-level Diagram

  17. 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

  18. 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

  19. 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.

  20. Questions?

  21. Arrays

  22. 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.

  23. 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)

  24. Terms • A cell or element represents one item in an array. • The index of a cell represents its location within the array.

  25. Visually Representing Arrays A cell at the fourth index. 1 2 3 4 5 6 7 8 9 10

  26. 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

  27. LB Declaring an Array Variable Like declaring any other variable: MyNumArray isoftype NumArrayType Variable name Type name

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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]

  34. 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

  35. 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

  36. Summary • Arrays • Are homogeneous collections • Are fixed in size • Are a linear data structure • Allow random, immediate access to elements

  37. Questions?

  38. Iteration

  39. 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.

  40. 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

  41. 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

  42. 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

  43. 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 ...

  44. 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.

  45. 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

  46. 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)

  47. 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)

  48. 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)

  49. 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)

  50. 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)

More Related