![Page 1: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/1.jpg)
September 12 1
An Algorithm for:Explaining Algorithms
Tomasz Müldner
![Page 2: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/2.jpg)
September 12 2
Vision = what is where by looking
Visualization = the power or process of forming a mental image of vision of something not actually present to the sight
You have 10s to find this image
![Page 3: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/3.jpg)
September 12 3
Dijkstra feared…
“…permanent mental damage for most students exposed to program visualization software …”
![Page 4: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/4.jpg)
September 12 4
Contents
• Preface
• Introduction to Algorithm Visualization, AV
• Examples of AV
• Algorithm Explanation, AE
• Examples of AE
• Conclusions & Future Work
![Page 5: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/5.jpg)
September 12 5
Preface
• Under Construction
• Early version• Invitation to collaborate
![Page 6: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/6.jpg)
September 12 6
Al-Khorezmi -> Algorithm
The ninth century:
• the chief mathematician in the academy of sciences in Baghdad
![Page 7: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/7.jpg)
September 12 7
Introduction to AV
AV uses multimedia:– Graphics– Animation– Auralization
to show abstractions of data
![Page 8: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/8.jpg)
September 12 8
Examples of AV
• Multiple Sorting• Duke• More• AIA
![Page 9: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/9.jpg)
September 12 9
Typical Approach in AV
• take the description of the algorithm• graphically represent data in the code using
bars, points, etc.• use animation to represent the flow of
control• show the animated algorithm and hope that
the learner will now understand the algorithm
![Page 10: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/10.jpg)
September 12 10
Problems with AV
• Graphical language versus text• Low level of abstraction (code stepping)• Emphasis on meta-tools• Students perform best if they are asked to
develop visualizations• no attempt to visualize or even suggest
essential properties, such as invariants • Very few attempts to visualize recursive
algorithms
![Page 11: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/11.jpg)
September 12 11
Introduction to AE
• systematic procedure to explain algorithms: an algorithm for explaining algorithms
• Based on findings from Cognitive Psychology, Constructivism Theory, Software Engineering
• visual representation is used to help reason about the textual representation
• Use multiple abstraction levels to focus on selected issues
• Designed by experts
![Page 12: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/12.jpg)
September 12 12
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 code the algorithm in any programming language
• Understanding of time complexity of the algorithm
![Page 13: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/13.jpg)
September 12 13
Requirements for AE
• The algorithm is presented at several levels of abstraction
• Each level of abstraction is represented by the abstract data model and pseudocode
• The design supports active learning• The design helps to understand time
complexity
![Page 14: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/14.jpg)
September 12 14
Levels of Abstraction
public static void selection(List aList) {
for (int i = 0; i < aList.size(); ++i) swap(smallest(i, aList) , i, aList);} Primitive operations can be:• Explained at different abstraction
level• inlined
![Page 15: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/15.jpg)
September 12 15
AE Catalogue Entries
• Multi-leveled Abstract Algorithm Model• Example of an abstract implementation of
the Abstract Algorithm Model• Tools that can be used to help to predict
the algorithm complexity• Questions for students
![Page 16: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/16.jpg)
September 12 16
MAK
• Uses multimedia:– Graphics– Animation– Auralization
to show abstractions of data• Interacts with the student; e.g. by providing
post-tests• Uses a student model for adaptive behavior
![Page 17: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/17.jpg)
September 12 17
MAK
Selection Sort
Insertion Sort
Quick Sort
![Page 18: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/18.jpg)
September 12 18
Selection Sort: Abstract Data Model
Sequences of elements of type T, denoted by Seq<T> with a linear order defined in one of three ways:
• type T supports the function
int compare(const T x)
• type T supports the “<” relation
• there is a global function
int comparator(const T x, const T y)
![Page 19: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/19.jpg)
September 12 19
Selection Sort: Top level of Abstraction
Abstract Data Model
Type T also supports the function
swap(T el1, T el2)
The following operations on Seq<T> are available:
• a sequence t can be divided into prefix and
suffix
• the prefix can be incremented (which will
decrement the suffix)
• first(suffix)
• T smallest(seq<T> t, Comparator comp)
![Page 20: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/20.jpg)
September 12 20
Selection Sort: Top level of Abstraction
Pseudocode void selection(Seq<T> t, Comparator comp) { for(prefix = NULL; prefix != t; increment prefix
by one element) swap( smallest(suffix, comp), first(suffix) );}
![Page 21: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/21.jpg)
September 12 21
void selection(Seq<T> t, Comparator comp) { for(prefix = NULL; prefix != t; increment prefix by
one element) swap( smallest(suffix, comp), first(suffix) );}
Visualization
List two invariants
![Page 22: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/22.jpg)
September 12 22
List two invariants
void selection(Seq<T> t, Comparator comp) { for(prefix = NULL; prefix != t; increment prefix by
one element) swap( smallest(suffix, comp), first(suffix) );}
![Page 23: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/23.jpg)
September 12 23
Selection Sort: Low level of Abstraction
Pseudocode T smallest(Seq<T> t, Comparator comp) { smallest = first element of t; for(traverse t forward) if(smallest & current are out of order) smallest = current;}
![Page 24: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/24.jpg)
September 12 24
T smallest(Seq<T> t, Comparator comp) {
smallest = first element of t; for(traverse t forward) if(smallest & current out of order) smallest = current;}
Visualization
![Page 25: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/25.jpg)
September 12 25
Abstract Implementation
The Abstract Iterator Implementation Model assumes
• There is an Iterator type, where iterations are performed over a half-closed interval [a, b)
• Iterator type supports the following operations:– two iterators can be compared for equality and
inequality– there are operations to provide various kinds of
traversals; for example forward and backward – an iterator can be dereferenced to access the object it
points to
![Page 26: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/26.jpg)
September 12 26
Abstract Implementation
The Abstract Iterator Implementation Model assumes (Cont.):
• The domain Seq<T> supports Seq<T>::Iterator
• The following two operations are defined on sequences:– Iterator t.begin() – Iterator t.end()
![Page 27: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/27.jpg)
September 12 27
Selection Sort: Abstract Implementation
Pseudocode void selection(Seq<T> t, Comparator
comp) { Seq<T>::Iterator eop; // end of prefix for(eop = t.begin(); eop != t.end(); +
+eop) swap(smallest(eop, t.end(), comp),
eop);} T smallest(Seq<T> t, Comparator comp) { Iterator small = t.begin(); Iterator current; for(current = t.begin(); current !=
t.end(); ++current) if(value of current < value of small)
small = current; return value of small;}
![Page 28: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/28.jpg)
September 12 28
void selection(T *x, T* const end, int comparator(const T, const T)) {
T* eop; for(eop = x; eop != end; ++eop) swap( smallest(eop, end, comparator), eop );}T *smallest(T * const first, T * const last, int comparator(const
T, const T) ) { T *small = first; T *current; for(current = first; current != last; ++ current) if(comparator(* current, *small) < 0) small = current; return s;}
void selection(Seq<T> t, Comparator comp) {Seq<T>::Iterator eop; // end of prefix for(eop = t.begin(); eop != t.end(); ++eop) swap(smallest(eop, t.end(), comp), eop);} T smallest(Seq<T> t, Comparator comp) { Iterator small = t.begin(); Iterator current; for(current = t.begin(); current != t.end(); +
+current) if(value of current < value of small) small =
current; return value of small;}
C implementation
![Page 29: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/29.jpg)
September 12 29
typedef struct { int a; int b;} T;#define SIZE(x) (sizeof(x)/sizeof(T))T x[ ] = { {1, 2}, {3, 7}, {2, 4}, {11, 22} };#define S SIZE(x)int comparator1(const T x, const T y) { if(x.a == y.a) return 0; if(x.a < y.a) return -1; return 1;}int comparator2(const T x, const T y) { if(x.a == y.a) if(x.b = y.b) return 0; else if(x.b < y.b) return -1; else return 1; if(x.a < y.a) return -1; return 1;}
int main() { printf("original sequence\n"); show(x, S); selection(x, x+S, comparator1); printf("sequence after first sort\n"); show(x, S); selection(x, x+S, comparator2); printf("sequence after second sort\n"); show(x, S);}
![Page 30: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/30.jpg)
September 12 30
template <typename Iterator, typename Predicate> void selection(Iterator first, Iterator last, Predicate
compare) { Iterator eop; for(eop = first; eop != last; ++eop) swap(*min_element(eop, last, compare), *eop); }
void selection(Seq<T> t, Comparator comp) {Seq<T>::Iterator eop; // end of prefix for(eop = t.begin(); eop != t.end(); ++eop) swap(smallest(eop, t.end(), comp), eop);} T smallest(Seq<T> t, Comparator comp) { Iterator small = t.begin(); Iterator current; for(current = t.begin(); current != t.end(); +
+current) if(value of current < value of small) small =
current; return value of small;}
C++ implementation
![Page 31: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/31.jpg)
September 12 31
public static void selection(List aList, Comparator aComparator) {
for (int i = 0; i < aList.size(); i++) swap(smallest(i, aList, aComparator) , i, aList);} private static int smallest(int from, List aList, Comparator
aComp) { int minPos = from; int count = from; for (ListIterator i = aList.listIterator(from); i.hasNext(); +
+count) if (aComp.compare(i.next(), aList.get(minPos)) < 0) minPos = count; return minPos;}
void selection(Seq<T> t, Comparator comp) {Seq<T>::Iterator eop; // end of prefix for(eop = t.begin(); eop != t.end(); ++eop) swap(smallest(eop, t.end(), comp), eop);} T smallest(Seq<T> t, Comparator comp) { Iterator small = t.begin(); Iterator current; for(current = t.begin(); current != t.end(); +
+current) if(value of current < value of small) small =
current; return value of small;}
Java implementation
![Page 32: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/32.jpg)
September 12 32
Algorithm Complexity
Three kinds of tools:
• to experiment with various data sizes and plot a function that approximates the time spent on execution with this data.
• a visualization that helps to carry out time analysis of the algorithm
• questions regarding the time complexity
![Page 33: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/33.jpg)
September 12 33
Post Test1. What is the number of comparisons and swaps performed when
selection sort is executed for:1. sorted sequence2. sequence sorted in reverse
2. What is the time complexity of the function isSorted(t), which checks if t is a sorted sequence?
3. Hand-execute the algorithm for a sample set of input data of size 4.4. Hand-execute the next step of the algorithm for the specified state5. What’s the last step of the algorithm?6. There are two invariants of this algorithm; which one is essential for
the correctness of swap(smallest(), eop), and why.7. “do it yourself “
![Page 34: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/34.jpg)
September 12 34
Quick SortPseudocode
void quick(Seq<T> t, Comparator comp) { if( size(t) <= 1) return; pivot = choosePivot(t); divide(pivot, t, t1, t2, t3, comp); quick(t1, comp); quick(t3, comp); concatenate(t, t1, t2, t3);}
![Page 35: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/35.jpg)
September 12 35
![Page 36: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/36.jpg)
September 12 36
![Page 37: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/37.jpg)
September 12 37
![Page 38: September 12 1 An Algorithm for: Explaining Algorithms Tomasz Müldner](https://reader035.vdocument.in/reader035/viewer/2022062511/551b393a550346ae7a8b4608/html5/thumbnails/38.jpg)
September 12 38
Future Work
• evaluation (eye movement?)
• student model
• different visualizations
• more complex algorithms
• Algorithmic design patterns
• generic approach
• MAK