# A New Approach to Learning Algorithms - Computer Science

AI and Robotics

Oct 29, 2013 (4 years and 6 months ago)

118 views

CATE, August 18, 2004

1

Yet another Way to Explain
Algorithms

Tomasz Müldner*,

Jodrey School of Computer Science, Acadia University,

* presenting

CATE, August 18, 2004

2

Standard approach

The user has to
map the problem
domain to the
graphical domain
and then looking
at the animation
they have to
retrieve essential
properties of the
algorithm.

CATE, August 18, 2004

3

Contents of the Talk

Introduction to Algorithm Visualization

Description of Algorithm Explanation

Example: Selection Sort

(Proceedings: Insertion Sort)

Conclusions

Future Work

CATE, August 18, 2004

4

Standard Algorithm Visualization

1.
take the
description

of the algorithm (usually
the code in a programming language, e.g. C)

2.
graphically represent
data

in the code using
bars, points, etc.

3.
use animation to represent the
flow of control

4.
show the animated algorithm

5.
hope that the learner will now
understand

the
algorithm

CATE, August 18, 2004

5

Algorithm Explanation (AE)

To make algorithm explanation possible,
the learner has to build a
mapping
:

AE uses a variety of tools to help the
students to learn algorithms, including
textual and visual representation

learner’s conceptions of
these entities and events

the domain consisting of the
algorithm entities and temporal
events

CATE, August 18, 2004

6

Goals of AE

Understanding of both,
what

the algorithm
is doing and
how

it works.

Ability to justify the algorithm correctness
(
why

the algorithm works).

Ability to
program

the algorithm in any
programming language.

Understanding the
time complexity

of the
algorithm.

CATE, August 18, 2004

7

Requirements of AE

the algorithm is presented at several
levels of abstraction

each level of abstraction is represented
by the text and optionally by visualization

active learning is supported

the design helps to understand time
complexity

presentations are designed by experts.

CATE, August 18, 2004

8

AE Explanations

An explanation of a single algorithm consists of the
following four parts:

Hierarchical Abstract Algorithm Model

Example of an abstract implementation of the
Abstract Algorithm Model

Tools to help predicting the algorithm
complexity.

Questions for students, including “do it yourself”
mode for each level of abstraction.

CATE, August 18, 2004

9

Example: Selection Sort

Abstraction tree

selection

smallest

swap

CATE, August 18, 2004

10

Top Level of Abstra
ction

ADT consists of sequences of elements of
type T, denoted by
Seq<T>
, with a
linear

order.

There is a function (or a type)

int comparator(const T x, const T y)

which returns
-
1 if x is less than y, 0 if they
are equal and +1 otherwise

CATE, August 18, 2004

11

Operations from top
-

prefix(t)
, possibly empty (NULL), which can be
incremented by one element;

inc(prefix(t))
, which increments a prefix by one
element;

suffix(t)
, where a prefix followed by the suffix is
equal to the entire sequence
t
;

first(suffix)
, which returns the first element of the
suffix;

T smallest(seq<T> t, Comparator comp)
, which
finds the smallest element in
t

(using
comp
);

swap(T el1, T el2)
, which swaps
el1

and
el2
.

CATE, August 18, 2004

12

Top Level Code: Text

void selection(Seq<T> t, Comparator comp)

{

for(prefix(t) = NULL; prefix(t) != t; inc(prefix(t)))

swap( smallest(suffix(t), comp), first(suffix(t)));

}

CATE, August 18, 2004

13

Visualization shows Invariants

INVARIANT 1

All elements in the prefix are smaller
(according to the “comp” relation)
than all elements in the suffix.

In the visualization, the prefix box is
smaller than the suffix box

INVARIANT 2

The prefix is sorted.

In the visualization, elements in the
prefix are growing

CATE, August 18, 2004

14

The ADT consists of data described before,
and the following operations:

first(t)
, which returns the first element of
the sequence
t
;

next(current, t)
, which returns the element
of the sequence
t
, following current, or
NULL if there is no such element.

CATE, August 18, 2004

15

Low Level Code: Text

T smallest(Seq<T> t, Comparator comp)

{

small = current = first(t);

while((current=next(current,t))!=NULL)

if(comp(small, current) < 0)

small = current;

return small;

}

CATE, August 18, 2004

16

Post Test

What is the number of comparisons and swaps
performed when selection sort is executed for a sorted
sequence and a sequence sorted in reverse.

What is the time complexity of the function isSorted(t),
which checks if t is a sorted sequence?

Hand
-
execute the algorithm for a sample set of input
data of size 4.

Hand
-
execute the next step of the algorithm for the
current state.

What’s the last step of the algorithm?

CATE, August 18, 2004

17

Conclusions

A new approach for learning algorithms:

an algorithm is explained at various levels of
abstraction

each level is designed to present a single
operation used in the algorithm

all operations are shown in a textual and visual
form

the visualization system presented in this work is
implemented using Macromedia Flash MX

CATE, August 18, 2004

18

Future Work

Generic visualizations for various classes
of algorithms such as iterative and
recursive

A complete system with a student model
to provide an intelligent and adaptive
learning system.

CATE, August 18, 2004

19

Correction

Kruskal

SET

PRIORITY
-
QUEUE

MERGE
-
FIND