do you remember what is the most - tarleton state university€¦ · stack example: towers of hanoi...

96

Upload: others

Post on 19-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia
Page 2: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Do you remember what is the most powerful tool for managing complexity?

2

Page 3: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Abstractions and more abstractions …

3You are here

Page 4: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Stacks

Stack = An ADT in which accesses are made at only one end

– LIFO = Last In First Out

– The insert is called Push and the removal is called Pop

4

Name three everyday

structures that are stacks

Page 6: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

6

Stack algorithms

Placing elements in the stack:

WHILE (more data)

Read value

push(myStack, value)

Removing elements from the stack:

WHILE (NOT IsEmpty(myStack))

value = pop(myStack)

Write value

Page 7: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ

A stack is initially empty. Draw the stack after each of these operations:

• push(42)

• push(15)

• push(10)

• pop()

• push(21)

• pop()

• pop()7EOL 0.5

Page 8: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Queues

Queue = An ADT in which items are inserted at one end and removed from the other end

– FIFO = First In First Out

– No standard queue terminology

• Enqueue, Enque, Enq, Enter, and Insertare used for the insertion operation

• Dequeue, Deque, Deq, Delete, and Removeare used for the removal operation.

8

Name

three

everyday

structures

that are

queues

Page 9: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Interesting trivia: queueing is one of only two words in the English language that has 5 vowels in a row!

E.g. Queueing Theory

4 vowels in a row is more common:

queue (!), aqueous, onomatopoeia, sequoia, archaeoastronomy, etc.

9

The other one is miaouing (miaoued?)

Page 10: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Queue algorithms

Placing elements in the queue:

WHILE (more data)

Read value

enque(myQueue, value)

Removing elements from the queue:

WHILE (NOT IsEmpty(myQueue))

value = deque(myQueue)

Write value

Page 11: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ

A queue is initially empty. Draw the queue after each of these operations:

• enque(42)

• enque(15)

• enque(10)

• deque()

• enque(21)

• deque()

• deque()11

Page 12: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Three levels of abstraction

What are the 3 levels of abstraction we use for data?

Page 13: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Stacks

A stack contains initially only the number 42. Draw the stack at the end of this sequence operations:

• push(10)

• push(11)

• pop()

• push(12)

• pop()

• push(101)

• push(102)

• pop()13

Page 14: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Stacks

This is the stack at the end:

• push(10)

• push(11)

• pop()

• push(12)

• pop()

• push(101)

• push(102)

• pop()

14

101

10

42

Page 15: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: ADT

What exactly does the stack ADT contain?

What exactly does the queue ADT contain?

Page 16: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: ADT

What exactly does the stack container contain?

– The data stored on that stack

– The functions push(), pop()

– The functions isFull(), isEmpty()

– Other functions (?)

Page 17: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Two implementations for ADTs

Array-based implementation

Objects in the container are kept in an array, i.e. physically next to each other in memory.

Linked-based implementation

Objects in the container are not kept physically together, but each item tells you where to go to get the next one in the structure

17

Did you ever play treasure hunt, a game in which each clue

tells you where to go to get the next clue?

Page 18: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

18

Memory maps for

Array implementation Linked implementation

Page 19: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

19

Stacks and Queues in the linked implementation

Page 20: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

20

Why access elements only at the ends?

Page 21: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Lists

• List are more general ADTs than stacks and queues

• There is an index for the current item (crt) that can move through the list.– Access takes place in position crt

21

Page 22: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

ListsOperations that can be applied to lists:

• Insert item New item into list at position crt– New item goes before old crt

– crt points to new item

• Delete item Remove the crt item from the list– Move crt left (except when at head of list)

• Get Next item Get the crt item– Move crt right (if at end of list set crt = NULL)

• More items Are there more items at crt?

• Reset Brings crt back to head of list

22

Page 23: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Linked-list implementation

23

crt

Page 24: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Array implementation of list

crt

Details in CS 241 - Data Structures!

Big problem:

insertion and

deletion

require shifting

the “tail” of the

array → time-

consuming!

Page 25: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Show the list and the outputs (if any)

after each of the following operations:

getNext()

getNext()

delete()

getNext()

insert(42)

moreItems()

getNext()

getNext()

getNext()

reset()

25

Page 26: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Algorithm for Creating and Printing Items in a List

26

WHILE (more data)

Read value

Insert(myList, value)

Reset(myList)

Write "Items in the list are "

WHILE (moreItems(myList))

GetNext(myList, nextItem)

Write nextItem, ' '

moveNext(myList)

Brings crt back to the head of the list

Page 27: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Algorithm for Creating and Printing Items in a List

27

WHILE (more data)

Read value

Insert(myList, value)

Reset(myList)

Write "Items in the list are "

WHILE (moreItems(myList))

GetNext(myList, nextItem)

Write nextItem, ' '

Trick question:

Which implementation is being used (array or linked)?

Brings crt back to the head of the list

Page 28: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

28

Logical Level

The algorithm that uses the list does not need to know how

the data in the list is stored (array or linked), or how the

various operations (Insert(), Reset(), moreItems()) are

implemented!

We have written algorithms using a stack, a queue, and a

list without ever knowing the internal workings, i.e. the

implementation of these containers.

Page 29: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

8.5 Trees

Structure such as lists, stacks, and queues are linear in nature; only one relationship is being modeled

Other (more or less complex) relationships require different structures

29

Page 30: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Binary Tree (BT)

30

Root node

Node with two children

Node with right child

Leaf node

Node with left child

EOL 1.5

Page 31: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

To do for next time:

• Read Sections 8.1, 8.2, 8.3

• End-of-chapter 1 – 5, 12, 13, 14

31

Page 32: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Binary tree = A linked container with a unique starting node called the root, in which each node can have up to two child nodes

• A node can have 0, 1, or 2 children

A unique path (series of nodes) exists from the root to every other node.

• There are no loops!

32

It is possible to implement BTs with

arrays, but …

Page 33: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ BT

33

Write the (unique) path from the root to the node containing:

• 7

• 8

• 6

Page 34: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Operations on lists:

• Insert item New item into list at position crt– New item goes before old crt

– crt points to new item

• Delete item Remove the crt item from the list– Move crt left (except when at head of list)

• Get next item Get the crt item– Move crt right (if at end of list set crt = NULL)

• More items Are there more items at crt?

• Reset Brings crt back to head of list

34

Page 35: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

insert(42)

delete()

moreItems()

getNext()

getNext(

delete()

delete()

delete()

getNext()

getNext() 35

QUIZ: Show the list (and crt) at the end of the

following sequence:

Page 36: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ BT

36

List all the nodes having:

• 0 children

• 1 child

• 2 children

Page 37: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Are these Trees? BTs?

37

Page 38: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Are these Binary Trees?

Page 39: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ CONCLUSIONS:

Trees• Have one (and only one) root

• Have directed edges

• There is one path from the root to any other node

• The path above is unique

In addition, Binary Trees have to satisfy:

• Any node can have only 0, 1, or 2 children

Page 40: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Putting a BT to work:Binary Search Tree (BST)

40

Do you notice a

pattern in this BT?

Page 41: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Binary Search Tree = A tree with the following properties:

1. Shape property → It is a binary tree

2. Semantic property→ For every node, the value stored in the node is

– greater than the value in any node in its left subtreeand

– smaller than the value in any node in its right subtree

41

x

<X >X

Page 42: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Are these Binary Search Trees?

Page 43: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Algorithm for searching an item in a BST

43

IsThere(tree, item)

IF (tree is null)

RETURN FALSE

ELSE

IF (item equals info(tree))

RETURN TRUE

ELSE

IF (item < info(tree))

IsThere(left(tree), item)

ELSE

IsThere(right(tree), item)

Recursive!

Page 44: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ BST:Search for item 18

44

IsThere(tree, item)

IF (tree is null)

RETURN FALSE

ELSE

IF (item equals info(tree))

RETURN TRUE

ELSE

IF (item < info(tree))

IsThere(left(tree), item)

ELSE

IsThere(right(tree), item)

Page 45: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ:

What’s the difference between a BT and a BST?

45

Page 46: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

46

Inserting an item in a BST: first we have to search for it!

Search for Kyrsten

in this tree …

Page 47: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

47

Inserting an item in a BST: first we have to search for it!

Search for Kyrsten

in this tree …

… and insert the item

where the search

ended!

Page 48: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

48

Insert(tree, item)

IF (tree is null)

Put item in a new tree

ELSE

IF (item < info(tree))

Insert (left(tree), item)

ELSE

Insert (right(tree), item)

Note how similar this algorithm

is to the search algorithm!

Page 49: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ on BST insertion

Problem 47/280:

The following elements are inserted in an initially empty BST:

50, 72, 96, 107, 26, 12, 11, 9, 2, 10, 25, 51, 16, 17, 95

Find the final tree.

49

Page 50: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Extra-credit QUIZ:

50

Page 51: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

To do for next time

Read section 8.4

End-of-chapter 21, 22

51EoL 2.5

Page 52: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ

• What do the acronyms BT and BST mean in

Computer Science?

• How is a BT similar to a BST?

• How is a BT different from a BST?

52

Page 53: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Binary Search Tree = A tree with the following properties:

1. Shape property → It is a binary tree

2. Semantic property→ For every node, the value stored in the node is

– greater than the value in any node in its left subtreeand

– smaller than the value in any node in its right subtree

53

x

<X >X

Page 54: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ on BST insertion

The following elements are inserted in an initially empty BST:

“Hello, world!”, “CS102”, “MATH120”, “PHYS122”, “ENGL111”, “ENGL112”

Find the final tree.

54

Page 55: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Printing/traversing a BST

55

Print(tree)

If (tree is not null)

Print (left(tree))

Write info(tree)

Print (right(tree))

Is that all there is to it? Yes!

Remember that recursive

algorithms can be very powerful

Page 56: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Printing/traversing a BST

56

Print(tree)

If (tree is not null)

Print (left(tree))

Write info(tree)

Print (right(tree))

This is called “in-order” traversal

Page 57: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Printing/traversing a BST

57

Print(tree)

If (tree is not null)

Print (left(tree))

Write info(tree)

Print (right(tree))

Apply the algorithm to this BST. Show all output.

Page 58: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

FYI: Other tree algorithms

Finding the depth of a tree:

Depth(tree)

IF (tree is null)

RETURN 0

ELSE

RETURN max(Depth(left(tree), Depth(right(tree)))

Finding the length of a tree:

Length(tree)

IF (tree is null)

RETURN 0

ELSE

RETURN 1 + Length(left(tree))+ Length(right(tree))

Page 59: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

8.6 Graphs

Graph = A data structure that consists of a set of nodes (called vertices) and a set of edges that connect nodes to each other.

Note: The “unique path” condition from trees has been removed; graphs are more general than trees!

59

Page 60: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Graphs: Unlike trees, there can be cycles and unconnected parts

60

Page 61: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Find 5 different cycles in this graph

61EOL3

Page 62: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: In each case, decide if the data structure is a tree or just a graph

62

Page 63: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Graphs: directed and undirected

63

Page 64: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Graphs

64

Page 65: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Representing a Graph: adjacency matrix

65

See Table 8.3/262

Our text simply calls it

“table”

Page 66: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ: Draw the adjacency matrix for this graph

66

Page 67: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Extra-credit QUIZ: If in a graph all edges are bi-directional, what property does the adjacency matrix have?

67

Page 68: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Graph Algorithms

68

Page 69: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Depth-First Searching Algorithm--Given a starting vertex and an ending vertex, we can develop an algorithm that finds a path from startVertex to endVertex

This is called a depth-first search because we start at a given vertex, we go to the deepest branch and explore as far down one path before taking alternative choices at earlier branches.

69

Page 70: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

DFS – Stack to the rescue!

70

startVertex endVertex

Push the start vertex

Page 71: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

DFS – Stack to the rescue!

71

startVertex endVertex

Pop the start vertex, then push its children (in alphabetical order)

Page 72: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

DFS – Stack to the rescue!

Figure 8.11 Using a stack to store the routes

72

startVertex endVertex

Unleash recursion!

Page 73: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

DFS – Nitty gritty: How to avoid loops?

Figure 8.11 Using a stack to store the routes

73

startVertex endVertex

Mark each node when we push it.

Page 74: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

DFS – Nitty gritty: When to stop?

Figure 8.11 Using a stack to store the routes

74

startVertex endVertex

A node is considered visited when popped (not when pushed)

Page 75: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ DFS: Find a path from Austin to Chicago

75

Show all the

steps!

Page 76: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Depth First Search(startVertex, endVertex)

Set found to FALSEPush(myStack, startVertex)WHILE (NOT IsEmpty(myStack) AND NOT found)

Pop(myStack, tempVertex)IF (tempVertex equals endVertex)

Write endVertexSet found to TRUE

ELSE IF (tempVertex not visited)Write tempVertexPush all unvisited vertices adjacent to tempVertexMark tempVertex as visited

IF (found)Write "Path has been printed"

ELSEWrite "Path does not exist")

76

And mark pushed vertex

as visited

Page 77: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Graph Algorithms

Depth-First Search (DFS) algorithm

It looks so easy – do we really need an algorithm for it?

77

Page 78: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

78Source: http://www.toofishes.net/blog/arch-package-visualization/

Page 79: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Is this the only path from Austin to Washington?

79

Page 80: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Is this the shortest path from Austin to Washington?

80

Page 81: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

What if there were a direct path Dallas → Washington?

81

1300

Page 82: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Conclusion on DFS: The algorithm is guaranteed to find a path (if at least one exists) between the given vertices

• Not all paths

• Not (in general) the shortest path

To find all, or the shortest path, we need to use different algorithms …

82

Page 83: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Other Graph Algorithms

• Breadth-First Search (BFS)

• Single-Source Shortest-Path Search (SSPS)

• All-Pairs Shortest Path Search

• Flow algorithms

• Etc. etc. etc.

83

Skip them in our text!

Page 84: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

84

SKIP8.7 Subprogram Statements

READ and take notes:

Ethical issues: Workplace Monitoring

Page 85: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Chapter Review Questions

• Distinguish between an array-based implementation and a linked implementation

• Distinguish between an array and a list

• Distinguish between and a unsorted list and a sorted list

• Distinguish between the behavior of a stack and a queue

• Distinguish between the binary tree and a binary search tree

85

Page 86: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Chapter Review Questions

• Draw the binary search tree that is built from inserting a series of items

• Understand the difference between a tree and a graph

• Use the DFS algorithm to find a path between two nodes of a graph.

86

Page 87: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Who am I?

87

In the 1940s, the new

computer architecture

I developed

revolutionized the

design of computers.

Today’s computers are

referred to as [my last

name] machines

because the

architectural principles

I described have proven

very successful.

Page 88: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Do you know?

88

What are some of the economic stimulus

scams uncovered by the IRS in 2007?

What is the Open Source Hardware Bank

(OSHB)? Explain its connection to

improving the function and expanding the

capabilities of hardware

How does graph theory relate to terrorist

detection?

Page 89: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Review exercises for Ch.8

31, 32, 34, 35, 37, 38, 47, 48, 5054 →“Table” means adjacency matrix.

56 → Instead of Sandler, let the destination be Fred.

The edges are all bidirectional (they go both ways). Remember that we push nodes on the stack in alphabetical order.

89

Page 90: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Remember from Ch.7:

Three types of abstractionData abstraction

Separation of the logical view of data from their implementation

Procedural abstraction

Separation of the logical view of actions from their implementation

Control abstraction

Separation of the logical view of a control structure from its implementation

Subprograms do this!

Unsigned integers can be implemented on 8,

16, 32, or 64 bits!

A for loop is the same in pseudocode, but can have different syntax

details in different languages!

Compilers/interpreters do it.

Page 91: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Abstract Data Type (ADT)

Abstract data type = A data type whose properties (data and operations) are specified independently of any particular implementation.

The terms container and black-box are used to describe ADTs.

91

Python strings and lists have their own methods

attached to them, e.g. my_list.sort()!

Page 92: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Three Views of Data

Application (user) level view = View of the data within a particular problem

The data is seen in terms of its physical properties and behaviors.

Example: A robot has 6 SONAR

sensors around its body. Each

SONAR measures a distance to

the nearest obstacle in front of it.

Page 93: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Three Views of Data

Logical (abstract) level view

View sees data objects as groups of objects with similar properties and behaviors

93

Example: The 6 distances

measured by SONARs are

stored in an array of integers.

The array has a length of 6, and

it supports initialization, printing,

searching, and sorting.

Page 94: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

Three Views of DataImplementation level view = A specific representation of the structure that hold the data, and the coding of the operations in a programming language

View sees the properties represented as specific data fields and behaviors represented as functions or methods implemented in code

Example: The array is implemented

as a Python list, with all the

operations, functions and methods (e.g. slicing, len(), append())

that lists have in Python.

Page 95: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ Three Views of Data

Describe how a word processor can be seen from the three views:

• user/application

• logical/abstract

• implementation (in Python)

95

Page 96: Do you remember what is the most - Tarleton State University€¦ · Stack example: Towers of Hanoi 5 Tower of Hanoi - Wikipedia, the free encyclopedia

QUIZ Three Views of Data

Three views of a word processor:

• user/application → There is a window on the screen, in which text appears. The user can place the cursor anywhere, and type of delete. Using menus toolbars, keyboard shortcuts …

• logical/abstract → document files, with titles, sections, bibliography, footnotes, …

• implementation (in Python) → functions for opening, reading and writing files, string variables to store lines and words of text, functions for searching and replacing text …

96