procedural programming & fundamentals of...

24
06.06.2018 1 Procedural Programming & Fundamentals of Programming Lecture 4 - Summer Semester 2018 Prof. Dr.-Ing. Axel Hunger & Joachim Zumbrägel 2 Writing a computer program is only one step in the Program Development Process Prof. Dr.-Ing. Axel Hunger

Upload: lynguyet

Post on 12-Aug-2018

253 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

1

Procedural Programming & Fundamentals of ProgrammingLecture 4 - Summer Semester 2018

Prof. Dr.-Ing. Axel Hunger & Joachim Zumbrägel

2

Writing a computer program is only one step in the Program Development Process

Prof. Dr.-Ing. Axel Hunger

Page 2: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

2

3

What we know so far... (1/3)

Computers should help us to solve problems

First we need to understand the problem, which requires to get all relevant information

Then we create a step-wise solution to it, which at its best is following a problem-solving strategy

Prof. Dr.-Ing. Axel Hunger

4

What we know so far... (2/3)

Prof. Dr.-Ing. Axel Hunger

By means of flowcharts (for algorithm) and pseudocode (for program) we document and structure our solution approach

A computer executes the program, which we write in a specific programming language

Page 3: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

3

5

What we know so far... (3/3)

Control structures serve to manage the order of instructions, which change data in the main memory of a computer.

To organize this data and its respective manipulation, we use data types and data structures.

The challenge is to develop a proper abstraction of the system in the real world into a problem model to be solved by the computer.

We are responsible for the programs that we write.

Prof. Dr.-Ing. Axel Hunger

Struct(ures)

Page 4: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

4

7

Records (Structures)

A record permits a programmer to handle a group of variables as one variableThe fields / members / components of a record can be any built-in or programmer-defined data typeA record can have values assigned to and read from it just like the built-in variable types

A record can also be passed as an argument to a function and serve as the return value for a function

Prof. Dr.-Ing. Axel Hunger

8

Structures in C

• A struct is a mechanism for grouping together related data items of different types.

Recall: An array groups items of a single type

• Example: We want to represent an airborne aircraft:

• We can use a struct to group these data together for each plane.

Prof. Dr.-Ing. Axel Hunger

Page 5: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

5

9

Defining a Struct

• We first need to define a new type for the compiler and tell it what our struct looks like.

• This tells the compiler how big our struct is and how the different data items (“members”) are laid out in memory.

• But it does not allocate any memory.

Prof. Dr.-Ing. Axel Hunger

10

Declaring and Using a Struct

time

altitud

e

flightNum: 409

flightNum: 1767

flightType planeA ;

planeA

planeB

flightType planeB ;

planeA . flightNum planeB . flightNum

planeA . airSpeed

planeA . altitude

planeA . time 

planeB . airSpeed

planeB . altitude

planeB . time

Access: Access:

==

Prof. Dr.-Ing. Axel Hunger

Page 6: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

6

11

Declaring and Using a Struct

• To allocate memory for a struct, we declare a variable“plane” using our new struct data type “struct flight type”.

• Memory is allocated, and we can access individual members of this variable:

• A struct’s members are laid outin the order specified by the definition.

Prof. Dr.-Ing. Axel Hunger

12

Airplane

Coordinates

Position time x y z

100s x0 y0 z0

200s x1 y0 z1

300s x2 y0 z2

400s x3 y0 z3

500s x4 y0 z4

600s x5 y0 z5

700s x6 y0 z6

800s x7 y0 z7

900s x8 y0 z8

1000s x9 y0 z9

Prof. Dr.-Ing. Axel Hunger

Page 7: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

7

13

Airplane

Coordinates(in km)

Position time x y z

100s 2 5 9

200s 4 6 8

300s

400s

500s

600s

700s

800s

900s

1000s

………………………………………

………………………………………

………………………………………

………………………………………

Prof. Dr.-Ing. Axel Hunger

14

Airplane

From the previous lecture: User‐defined data type

Define an array of type flightType:

flightType t[10];

time IndexElement

ofArray  t

Cells in Main 

Memory

100s 0 t [0]

200s 1 t [1]

300s 2 t [2]

400s 3 t [3]

500s 4 t [4]

600s 5 t [5]

700s 6 t [6]

800s 7 t [7]

900s 8 t [8]

1000s 9 t [9]At

100sAt

200sAt

300sAt

400sAt

500sAt

600sAt

700sAt

800sAt

900sAt

1000s

Prof. Dr.-Ing. Axel Hunger

Page 8: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

8

15

Declaration of User-defined Data Type

struct student

{

int matr_num;

char family_name [20];

char first_name [20];

char study_course [20];

} ;

components of the structure withdifferent data types

keyword to start the structure declaration

name of the structure

Prof. Dr.-Ing. Axel Hunger

16

Access to Structure Components

student_inStudyCourse . matr_num

student_inStudyCourse . family_name

student_inStudyCourse . first_name

student_inStudyCourse . study_course

Cells in Main 

Memory

Access to each component

student_inStudyCourse is a structured variableconsisting of four components.

struct student

{

int matr_num;

char family_name [20];

char first_name [20];

char study_course [20];

};

struct student student_inStudyCourse ;

Prof. Dr.-Ing. Axel Hunger

Page 9: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

9

Dynamic Data Structures- Linked List -

18

Linked List

• is a data structure in which each element is dynamically allocated and in which elements point to each other to define a linear relationship

• Singly- or doubly-linked

• Stack, queue, circular list

Prof. Dr.-Ing. Axel Hunger

Page 10: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

10

19

Definition of Linked List (1/2)

Prof. Dr.-Ing. Axel HungerProf. Dr.-Ing. Axel Hunger

struct listItem {struct student student_inStudyCourse;struct listItem *next;

};

20

Definition of Linked List (2/2)

• Items of list are usually same type

• Each item points to next item

• Last item points to null

• Need “head” to point to first item!

Prof. Dr.-Ing. Axel HungerProf. Dr.-Ing. Axel Hunger

Page 11: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

11

21

Head of a Linked List

Prof. Dr.-Ing. Axel HungerProf. Dr.-Ing. Axel Hunger

struct listItem {struct student student_inStudyCourse;struct listItem *next;

};struct listItem *head;

22

Usage of Linked Lists

• Not massive amounts of data

• Linear search is okay

• Sorting not necessary

• or sometimes not possible

• Can add and delete data “on the fly”

• Even from middle of list

Prof. Dr.-Ing. Axel Hunger

Page 12: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

12

23

Adding an Item to a List (1/3)

• Add an item pointed to by q after item pointed to by p• Neither p nor q is NULL

Prof. Dr.-Ing. Axel HungerProf. Dr.-Ing. Axel Hunger

q

p

24

Adding an Item to a List (2/3)

• Add an item pointed to by q after item pointed to by p

Prof. Dr.-Ing. Axel Hunger

q

p

q.next = p.next

Page 13: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

13

25

• Add an item pointed to by q after item pointed to by p

Adding an Item to a List (3/3)

Prof. Dr.-Ing. Axel Hunger

q

p

p.next = q

Data structuresapplied in problem solving

Page 14: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

14

27

How to cross the river?

Prof. Dr.-Ing. Axel Hunger

28

Mapping information to data

Prof. Dr.-Ing. Axel Hunger

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

a state indicating thelocation of items

• 0: item is at ‚near shore‘• 1: item is at ‚far shore‘

int SHORE[4]={0,0,0,0};

Condition on state transition: Boat can only carry one or none item at a time!‐> relevant to identify neighboring states

Page 15: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

15

29

State space model (24 = 16 states)

Prof. Dr.-Ing. Axel Hunger

(1): START

Boat:  0

Fox:    0

Goose: 0

Corn:  0

(16): END

Boat:  1

Fox:    1

Goose: 1

Corn:  1

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

30

State space model (10 valid/ 6 invalid states)

Prof. Dr.-Ing. Axel Hunger

START

Boat:  0

Fox:    0

Goose: 0

Corn:  0

END

Boat:  1

Fox:    1

Goose: 1

Corn:  1

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Page 16: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

16

Breadth-first search

32

Breadth-first search (BFS)

• belongs to uninformed (or blind) searching strategies in a graph (a structure made up of nodes connected with edges)

• does not make use of heuristics for minimizing its computational expensiveness in solving a problem, such asin informed searching approaches

• begins at a root node and inspects all the neighboring nodes before expanding next level

• does not explore graph as far as possible along one path before backtracking, such as in depth-first search

• makes use of queue to manage graph traversal

Prof. Dr.-Ing. Axel Hunger

Page 17: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

17

33

Queue

• is a data structure following the FIFO (First In First Out) principle to its elements

• can be implemented by linked list

Prof. Dr.-Ing. Axel Hunger

HEAD TAIL1 TAIL… TAILnFRONT BACK

TAILn+1enqueue

HEADdequeue

Queue

34

An algorithm for breadth-first search

1. Determine START state and add it to QUEUE.

2. Pick head state from QUEUE, add it to QUEUE _visited.

a) IF ‚head==END‘ then stop and „success“.b) ELSE add all neighboring states of head

to (tail of) QUEUE.3. IF ‚QUEUE == empty‘ then „fail“.

4. Repeat 2.

Page 18: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

18

35

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 1 of 10

Prof. Dr.-Ing. Axel Hunger

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

QUEUE START

QUEUE_visited

Head of QUEUE Tail of QUEUE

36

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 2 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE START 5 6 7 8

QUEUE_visited START

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Strategy #1: Do not consider invalid states.

Page 19: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

19

37

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 3 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE 7 START 3

QUEUE_visited START 7

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Strategy #2: Do not consider already visited states again.

38

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 4 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE 3 7 9 10

QUEUE_visited START 7 3

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Page 20: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

20

39

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 5 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE 9 10 12 3 4

QUEUE_visited START 7 3 9

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Note: QUEUE_visited does NOT equals THE solution path!

40

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 6 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE 10 4 14 2 3

QUEUE_visited START 7 3 9 10

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Page 21: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

21

41

Step 7 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE 4 2 6 9 15

QUEUE_visited START 7 3 9 10 4

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

already close to END, but...

42

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 8 of 10

Prof. Dr.-Ing. Axel Hunger

QUEUE 2 15 8 (15) 10

QUEUE_visited START 7 3 9 10 4 2

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2

0 0 0 1

3

0 0 1 0

4

0 1 0 0

5

1 0 0 0

6

1 1 0 0

7

1 0 1 0

8

1 0 0 1

9

1 1 1 0

10

1 0 1 1

12

0 1 1 0

13

0 1 0 1

14

0 0 1 1

15

1 1 0 1

11

0 1 1 1

Strategy #3: Do not consider same head state twice.

Page 22: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

22

43

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 9 of 10

Prof. Dr.-Ing. Axel Hunger

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2:0001

0 0 0 1

3:0011

0 0 1 0

4:0100

0 1 0 0

5:0101

1 0 0 0

6:0110

1 1 0 0

7:0111

1 0 1 0

8:1000

1 0 0 1

9:1001

1 1 1 0

10:1010

1 0 1 1

12:1100

0 1 1 0

13:1101

0 1 0 1

14:1110

0 0 1 1

15:1111

1 1 0 1

11:1011

0 1 1 1

QUEUE 15 13 2 4

QUEUE_visited START 7 3 9 10 4 2 15

44

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Step 10 of 10

Prof. Dr.-Ing. Axel Hunger

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

2:0001

0 0 0 1

3:0011

0 0 1 0

4:0100

0 1 0 0

5:0101

1 0 0 0

6:0110

1 1 0 0

7:0111

1 0 1 0

8:1000

1 0 0 1

9:1001

1 1 1 0

10:1010

1 0 1 1

12:1100

0 1 1 0

13:1101

0 1 0 1

14:1110

0 0 1 1

15:1111

1 1 0 1

11:1011

0 1 1 1

END

Boat:  1

Fox:    1

Goose: 1

Corn:  1

QUEUE 13 15 END

QUEUE_visited START 7 3 9 10 4 2 15 13

Page 23: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

23

45

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Final Step: „Success“

Prof. Dr.-Ing. Axel Hunger

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2:0001

0 0 0 1

3:0011

0 0 1 0

4:0100

0 1 0 0

5:0101

1 0 0 0

6:0110

1 1 0 0

7:0111

1 0 1 0

8:1000

1 0 0 1

9:1001

1 1 1 0

10:1010

1 0 1 1

12:1100

0 1 1 0

13:1101

0 1 0 1

14:1110

0 0 1 1

15:1111

1 1 0 1

11:1011

0 1 1 1

QUEUE END

QUEUE_visited START 7 3 9 10 4 2 15 13 END

46

1. Determine START state and add it to LIST.

2. Pick head state from LIST and add it to LIST_visited.

a) IF ‚head==END‘ then stop and „success“.

b) ELSE add all (valid) next states to (tail of) LIST.

3. IF ‚LIST == empty‘ then „fail“.

4. Repeat 2.

Final Step: „Success“

Prof. Dr.-Ing. Axel Hunger

QUEUE END

QUEUE_visited START 7 3 9 10 4 2 15 13 END

START

Boat:  0 Fox:    0 Goose: 0 Corn:  0

END

Boat:  1 Fox:    1 Goose:

2:0001

0 0 0 1

3:0011

0 0 1 0

4:0100

0 1 0 0

5:0101

1 0 0 0

6:0110

1 1 0 0

7:0111

1 0 1 0

8:1000

1 0 0 1

9:1001

1 1 1 0

10:1010

1 0 1 1

12:1100

0 1 1 0

13:1101

0 1 0 1

14:1110

0 0 1 1

15:1111

1 1 0 1

11:1011

0 1 1 1

the „tricky“ step

Page 24: Procedural Programming & Fundamentals of Programmingti.uni-due.de/.../teaching/ss18/foppp/FOPPP-SS2018-Lecture4.pdf · 06.06.2018 1 Procedural Programming & Fundamentals of Programming

06.06.2018

24

47

Finally…

• Applying strategies can reduce time and space complexity of a solution approach

• Developing solutions is no mechanic procedure, it is a creative act

• For problem solvers and programmers to be effective:

There is not just one way of doing it.

Prof. Dr.-Ing. Axel Hunger

END of 6th lecture