Graphs Arrays Iteration Combining Data Structures.

Slides:



Advertisements
Similar presentations
Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree.
Advertisements

Linked List: Traversal Insertion Deletion. Linked List Traversal LB.
Tonight’s JavaScript Topics 1 Conditional Statements: if and switch The Array Object Looping Statements: for, while and do-while.
Computer Science 1620 Loops.
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Chapter 5: Loops and Files.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Searching via Traversals Searching a Binary Search Tree (BST) Binary Search on a Sorted Array Data Structure Conversion and Helper Modules.
Optimization Problems Minimum Spanning Tree Behavioral Abstraction.
Chapter 4: Control Structures II
Fundamentals of Python: From First Programs Through Data Structures
Algorithm Cost Algorithm Complexity. Algorithm Cost.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
Introduction to Data Structures. Definition Data structure is representation of the logical relationship existing between individual elements of data.
Chapter 2 - Algorithms and Design
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Data Strcutures.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 9 Arrays.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Algorithm Analysis (Algorithm Complexity). Correctness is Not Enough It isn’t sufficient that our algorithms perform the required tasks. We want them.
CSC 211 Data Structures Lecture 13
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Visual Basic Programming
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 4: Control Structures II
Class Examples (Simple, Airplane, Queue, Pile) Copy vs. Clone.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
Review Records Dynamic Memory and Pointers Introduction to Linked Lists.
A loop is a repetition control structure. body - statements to be repeated control statement - decides whether another repetition needs to be made leading.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
Pascal Programming Iteration (looping) Carl Smith National Certificate Unit 4.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Review Problems. What is the Big O? i
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Stacks Queues Introduction to Trees. Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
 Python for-statements can be treated the same as for-each loops in Java Syntax: for variable in listOrstring: body statements Example) x = "string"
Data Structures I (CPCS-204)
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 5: Control Structures II
Lecture 14 Traversals of Linked Lists Preorder BST Traversal Postorder BST Traversal Miscellaneous BST Traversals Depth First vs Breadth First Array Traversals.
LOOPS.
Arrays … The Sequel Applications and Extensions
Arrays, For loop While loop Do while loop
Linear and Binary Search
Outline Altering flow of control Boolean expressions
Introduction to Problem Solving and Control Statements
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Graphs Arrays Iteration Combining Data Structures

Graphs

The Scenario Imagine we need to represent a highway system within our algorithm. Atlanta Chattanooga Tampa Birmingham 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 F C A E G D

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

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

Undirected Graphs B F C A E G D

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

Weighted Edges B F C A E G D Edge weights represent cost.

Directed Graphs Can be Weighted Too B F C A E G D

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 children data children data children data children datanext_child this_child next_child this_child next_child this_child...

Another View Node 1 Children Node 2 Children Node 3 Children Node 4 Children Node 2Node 3Node 4Node 1Node 2 Node This represents a pointer to node 3 LB

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

Defining Arrays Like a record definition, we define a new data type: MAX is 10 NumArrayType definesa Array [1..MAX] of Num Constant size Type name Bounds/Range Cell type LB

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

Accessing an Element in an Array Use brackets “[ ]” and specify an index value within the bounds: MyNumArray[4] <- 42 An array A number

Multi-Dimension Arrays 2-D 3-D D & beyond can do, but visually ???

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 Column My2DNumArray My2DNumArray isoftype 2DNumArrayType

Accessing Elements in a 2-D Array Row Column My2DNumArray

Accessing Elements in a 2-D Array Row Column My2DNumArray My2DNumArray[3]

Accessing Elements in a 2-D Array Row Column My2DNumArray My2DNumArray[3][8] < or – My2DNumArray[3,8] <- 31 LB

Using Bounds Correctly 31.. [3][8] [8][3] (out of bounds) Row Column

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 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( ) 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 sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 10

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 11

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 21

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 21

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 21

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 23

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 33

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 33

Time Passes...

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 33

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 36

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 46

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 46

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 46

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 410

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 510

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 510

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 510

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 515

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 615

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 615

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 615

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 621

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 721

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 721

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 721

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 728

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 828

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 828

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 828

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 836

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 936

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 936

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 936

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 945

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1045

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1045

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1045

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1055

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1155

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1155

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1155

Tracing the Loop’s Behavior i sum sum <- 0 // initialize i <- 1 // initialize loop exitif (i > 10) sum <- sum + i // work i <- i + 1 // increment endloop print(sum) 1155

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 Iteratively repeat until we reach nil ListHead //

Start With a Framework loop ??? exitif(???) ??? endloop

Initialize current isoftype ptr toa Node current <- ListHead // from elsewhere loop ??? exitif(???) ??? endloop

Do the Work current isoftype ptr toa Node current <- ListHead // from elsewhere loop current^.data <- current^.data * 2 exitif(???) ??? endloop

Move Closer to the End current isoftype ptr toa Node current <- ListHead // from elsewhere loop current^.data <- current^.data * 2 current <- current^.next exitif(???) ??? endloop

When Can We Finish? current isoftype ptr toa Node current <- ListHead // 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 <- ListHead // 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 <- ListHead // 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( ) endloop

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 LB

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

Test-Last Loops Loops which have the exitif as the last line in the loop body. The loop executes at least once. loop exitif( ) endloop Typically used when This Is generated in here LB

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() // displays menu read(choice) // reads user’s choice exitif(???) ??? endloop

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 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 exitif( ) 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 NumArray definesa array [1..MAX] of num Node definesa record data isoftype NumArray 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 ListArray 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 of Linked Lists A Linked List of Trees of Arrays Etc.

Questions?