You are on page 1of 124

Programming, Algorithms

and Data Structures

Dr. Diana Hintea


Lecturer in Computer Science

Coventry University, UK

05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Learning Outcomes
Object Oriented
Programming
Concepts

Algorithms
and their
Complexity

Recursion
Sorting
Algorithms
Linked Lists,
Trees and Graphs
05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts
Many languages support a method called object oriented
programming.
This method allows you to break a program up into objects.
Objects are pieces of code that consist of both functions and data.
Multiple functions and data items can be combined into an object.
Objects can also be reused.
Objects will often represent real objects that the program is
trying to represent e.g. employee, or student.

05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Classes and objects

When you define an object you are really defining a class of objects.
A class acts like a template for an object.
A class on its own does nothing.
But once you have defined a class you can create many
instances of it.
These are called objects.
Objects are what do things and store data.

05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Main concepts

Encapsulation
Inheritance
Polymorphism
Abstraction

05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Inheritance
A class can use the member variables and methods of another class.
This is achieved through a mechanism called inheritance.
Inheritance can be thought of as specialising a general case into
a more specific case.
For example, a car, or a bike, is a specialisation of a vehicle, which
is more general class.

05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts
public class Bicycle
{
public int gear;
public int speed;
public Bicycle(int startSpeed, int startGear)
{
gear = startGear;
speed = startSpeed;
}
05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts
public void setGear(int newValue)
{
gear = newValue;
}
public void applyBrake(int decrement)
{
speed -= decrement;
}
public void speedUp(int increment)
{
speed += increment;
} }
05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts
public class MountainBike extends Bicycle {
public int seatHeight;
public MountainBike(int startHeight,
int startSpeed,
int startGear) {
super(startSpeed, startGear);
seatHeight = startHeight;
}
public void setHeight(int newValue)
{
seatHeight = newValue;
}
}
05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Abstract class
A class which contains the abstract keyword in its declaration is
known as abstract class.
Abstract classes may or may not contain abstract methods ie.,
methods with out body ( public void get(); )
But, if a class have at least one abstract method, then the
class must be declared abstract.
If a class is declared abstract it cannot be instantiated.
To use an abstract class you have to inherit it from another class,
provide implementations to the abstract methods in it.
If you inherit an abstract class you have to provide implementations
to all the abstract methods in it.
05.09.2015

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Abstract class
abstract class GraphicObject
{
int x, y;
void moveTo(int newX, int newY) { ... }
abstract void draw();
abstract void resize();
}

05.09.2015

10

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Abstract class
class Circle extends GraphicObject
{
void draw() { ... }
void resize() { ... }
}

class Rectangle extends GraphicObject


{
void draw() { ... }
void resize() { ... }
}
05.09.2015

11

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Encapsulation
Encapsulation in Java is a mechanism of wrapping the data (variables)
and code acting on the data (methods) together as a single unit.
In encapsulation the variables of a class will be hidden from other
classes, and can be accessed only through the methods of their
current class, therefore it is also known as data hiding.
To achieve encapsulation in Java
1. Declare the variables of a class as private.
2. Provide public setter and getter methods to modify and view the
variables values.
05.09.2015

12

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts
public class EncapTest
{
private String name;
private int age;
public int getAge() { return age; }
public String getName() { return name; }

public void setAge( int newAge) { age = newAge; }


public void setName(String newName) { name = newName; }
}
05.09.2015

13

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

The public setX() and getX() methods are the access points of the
instance variables of the EncapTest class.
Normally, these methods are referred as getters and setters.
Therefore any class that wants to access the variables should access
them through these getters and setters.

05.09.2015

14

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

Polymorphism
Polymorphism is when several classes have the same interface,
often inherited from a base class. E.g. all shapes would have an
area method, though the implementation is different for each shape.
Polymorphism is the ability of an object to take on many forms.
The most common use of polymorphism in OOP occurs when a parent
class reference is used to refer to a child class object.
Any Java object that can pass more than one IS-A test is considered
to be polymorphic.
05.09.2015

15

Data Fusion between a 2D Laser Profile Sensor and a Camera

Object Oriented
Programming Concepts

public interface Vegetarian{}


public class Animal{}
public class Deer extends Animal implements Vegetarian{}
The Deer class is considered to be polymorphic since this has
multiple inheritance. Following are true for the above example:
A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A Deer
A Deer IS-A Object
When we apply the reference variable facts to a Deer object reference,
the following declarations are legal:
Deer d = new Deer(); Animal a = d; Vegetarian v = d; Object o = d;
05.09.2015

16

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


What is an algorithm?

A way of doing things.


A finite list of instructions.
An effective method.
Made of well-defined steps.
A process of turning an input state into an output state.
All of the above.

05.09.2015

17

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


What do we need to express them?
Almost anything.
We use the ideas of Turing Completeness and
Turing equivalency to describe computers, processors and
programming languages (all the same thing from a certain
abstract view)
To be equivalent to all other machines, we need:
1. Conditional branching.
2. Modifiable state.

05.09.2015

18

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


What do we mean by modifiable?
Modifiable state:
1. Variables or just plain memory access.
2. Or a disk.
3. Or any other way to hold data.
A program can be described as a series of changes to state.
The data of a program can be referred to as its "state space.
If we can't remember any numbers or change some output,
we can't do much.

05.09.2015

19

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


Pseudocode!

Close to a programming language.


Provides a language independent way to describe an algorithm.
Formal enough to convert into any programming language.
Example:
INSERTION-SORT(A)
for i 1 to length[A]
key A [i]
ji
while j > 0 and A [j - 1] > key
A [j] A [j - 1]
jj-1
A[j] key
05.09.2015

20

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


A few rules
1. Assignment
Pseudo-code uses for assignment instead of =
Sometimes written as < = is used for comparison instead of ==
Multiple assignment is achieved with: ab0
(a and b are variables)
Arrays in Pseudo-code sometimes start at 1, unlike most
programming languages which start at 0.

05.09.2015

21

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


A few rules
2. Indentation
Code blocks are defined with indentation, this will be familiar
to Python programmers.
This also applies to loops and functions.
3. Loops and conditionals
Loops, such as while and for, retain their commonly understood
meanings, as do if and else statements.
Loop counters retain their value after the loop has finished.
4. Functions
Capitalise function name, include parameters in brackets
DO-STUFF(THING1, DATE)
05.09.2015

22

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


Nice video on algorithms

https://www.youtube.com/watch?v=6hfOvs8pY1k

05.09.2015

23

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity

05.09.2015

24

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity

05.09.2015

25

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity

05.09.2015

26

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity

05.09.2015

27

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


Big O notation
Makes it easier to analyse algorithm efficiency.
Focuses on essential features without worrying about details.
Real run-time is heavily influenced by characteristics of the machine
running the algorithm.
When using this notation, we are ignoring the actual run-time in favour
of a measure of efficiency not dependent on a particular platform.
We assume a single line of pseudo-code takes constant time.
We also assume that the line which controls a loop takes constant time,
though the loop itself may not.
Data types are assumed to have finite magnitude and precision.
05.09.2015

28

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


Runtime analysis
Depends on several things:
Input size.
Input content.
Upper bounds.
Many algorithms can exit when the correct conditions are met;
this maybe on the first run, or it may have to search the whole
data structure.
For analysis, we always consider the worst case.

05.09.2015

29

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


The notation
O(): This represents an algorithm whose performance will drop
linearly with the size of the input. For example, with an input size
of 20 it will take twice as long as with an input of 10.
O(2 ): This represents an algorithm whose performance will drop
exponentially with input size. For example, with an input size of
20 it will take 300 times as long as with an input of 10.
O(log()): This represents an algorithm whose performance will
drop as the log of the input.

05.09.2015

30

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


Example
INSERTION-SORT(A)
for j 1 to length[A]
key A[j]
ij-1
while i > 0 and A[i] > key
A[i+1] A[i]
ii+1
A[i+1] key

05.09.2015

(n times)
(n times)
(n times)
(n*n times)
(n*n times)
(n*n times)
(n times)

31

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


What have we learnt so far?
The most expensive parts of the algorithm are the loops.
Nested loops get exponentially more expensive.
So one loop executes n times, two nested loops execute n2 times,
three nested loops execute n3 times, etc.

05.09.2015

32

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


In more detail
Big O Notation is concerned mainly with the size of arrays and how
often the algorithm needs to iterate through them.
Always look at worst case.
Used for comparing "growth" - how does input size affect runtime?
Another example:
MAX(list)
max list[0]
(1)
for i list[1] .. list[length(list)-1]
(n)
if i > max
(n)
max i
(n)
return max
(1)
05.09.2015

33

Data Fusion between a 2D Laser Profile Sensor and a Camera

Algorithms and their Complexity


In more detail
The sum becomes 1+n+n+n+1=3n+2
Not sure how many times we find a larger number so, let's say all of
the time, thats why n is associated to the max change.
So, we worked out 3n+2, but we would just write it in big-O
notation as O(n).
We ignore all constants and multipliers not related to input size, as
we've seen. But we also ignore lower order terms, so if we
had n + 2 , we'd just write it as O(2 ).

05.09.2015

34

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 1
Write the functions below in order of asymptotic growth rate,
beginning with the largest. If any of the functions have the same
growth rate, be sure to show it.

42
4
+
1.5
3log()
40
log()
05.09.2015

35

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 1 Solution

4 , 42 , 1.5 , 3log(),[ + , 40] , log()

05.09.2015

36

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 2
Examine the graph below. The plots A, B, C, D, E represent the growth
rates log(), 2 , 4 , 20, 3. State the function for each plot.

05.09.2015

37

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 2 Solution

A 2
B 3
C 20
D log()
E 4

05.09.2015

38

Data Fusion between a 2D Laser Profile Sensor and a Camera

Recursion
What is recursion?
A recursive function calls itself.
It must have a condition to stop this process.
This is the base case.
Example: 0 = 1 is always true.
Every other case should take us toward the base case.
This is the recursive case
Example: = 1 for all x>1

05.09.2015

39

Data Fusion between a 2D Laser Profile Sensor and a Camera

Recursion
Example
Factorial:
5! = 5 = 5 4 3 2 1
FACTORIAL(n)
if(n < 2)
return 1
else
return FACTORIAL(n-1) * n
The recursion stops when n is 1.

05.09.2015

40

Data Fusion between a 2D Laser Profile Sensor and a Camera

Recursion
Example

05.09.2015

41

Data Fusion between a 2D Laser Profile Sensor and a Camera

Recursion
Another example
Binary search:
http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html
BINARY_SEARCH(A, v, first, last)
if last < first
return false
i first + (last - first)/2
if A[i] == v
return true
if A[i] > v
BINARY_SEARCH(A,v,first,i-1)
else
BINARY_SEARCH(A,v,i+1,last)
05.09.2015

//Base case 1

//Base case 2

//Recursion case 1
//Recursion case 2
42

Data Fusion between a 2D Laser Profile Sensor and a Camera

Recursion
Another example
Towers of Hanoi:
http://www.dynamicdrive.com/dynamicindex12/towerhanoi.htm

05.09.2015

43

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Introduction
Sorting data is a fundamental problem in software development
and also a core topic in computer science.
If we consider that the "information age" is all about data,
and we have huge amounts of it then dealing with the data
efficiently is critical.
Today, we will be looking at a selection of sorting algorithms.

05.09.2015

44

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Bogosort

Is the list in order?


If not, shuffle it and try again.
If it is, we're done.
Best case isn't bad: O(n)
Average case is crazy O((n+1)!)
Worst case isforever.

05.09.2015

45

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Bogosort

05.09.2015

46

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Insertion sort
Pick the first item.
Keep swapping it with its left neighbour until the swap would put
it to the left of a number smaller than it.
Pick the next number along.
Repeat.

05.09.2015

47

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Insertion sort

05.09.2015

48

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Insertion sort
INSERTION-SORT(A)
for i 1 to length[A]
key A[i]
ji
while j > 0 and A[j-1] > key
A[j] A[j-1]
jj-1
A[j] key
Best case performance: O()
Worst case performance: O(2 )
Average case performance: O(2 )
05.09.2015

49

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Bubble sort
Examine each consecutive pair of items.
If they are out of order, swap them.
Repeat until no swaps are made.

05.09.2015

50

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Bubble sort

05.09.2015

51

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Bubble sort
BUBBLE(A)
swapTrue
while swap is True:
swapFalse
for i 0 to length(A)-1
if A[i] > A[i+1]
swapTrue
swap(A[i],A[i+1])
Best case performance: O()
Worst case performance: O(2 )
Average case performance: O(2 )
05.09.2015

52

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort
Divide the unsorted list into n sublists, each containing 1 element
(a list of 1 element is considered sorted).
Repeatedly merge sublists to produce new sorted sublists until
there is only 1 sublist remaining.
This final list will be the sorted list.

05.09.2015

53

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort

05.09.2015

54

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort
MERGE-SORT(l):
if length(l) <= 1
return l
al[0 .. length(l)/2]
bl[length(l)/2 .. length(l)]
aMERGE-SORT(a)
bMERGE-SORT(b)
return MERGE(a,b)

05.09.2015

55

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort
MERGE(a,b)
out array of length length(a)+length(b)
i0
j0
for x 0 length(out)
if a[i] < b[j]
out[x] = a[i]
i++
else
out[x] = b[j]
j++
05.09.2015

56

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort

Best case performance: O()


Worst case performance: O()
Average case performance: O()
The merge operation is clearly O(n) (where n is the combined
length of the two inputs) because it simply iterates once over
them.
But how many times is it called?
The proof we used before to show the binary search
was O(logn) still applies, but now it's used to show that we apply
the merge logn times and so we have O(nlogn).
05.09.2015

57

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort

Draw the divisions in a tree (or imagine doing so).


Each layer has n elements that must be processed.
There are logn layers to process.
So, process n items logn times

05.09.2015

58

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Quick sort
Pick a "pivot.
Create two empty lists, one for all items less than the pivot, one
for all greater.
For each item in the original list, move it to the correct new list.
Now sort each half (by applying this all over again).

05.09.2015

59

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Quick sort

05.09.2015

60

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Quick sort
function QUICKSORT(A)
if length(A)<=1
return A
pivot A[?]
L []
G []
for each x in array
if x pivot
append x to L
else
append x to G
return concatenate(quicksort(L), A[pivot],quicksort(G))
05.09.2015

61

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Quick sort
Best option: pick the value int he middle of the range.
But how? If we have to hunt through to be sure, it can slow things
down a lot.
So pick the first or last item? If the data isn't sorted, it doesn't
matter, so this would be fine.
Except: what if it is sorted? If it's sorted, then we end up
with O(n2) performance.
Take the one in the middle? Better, but could be given data made
specifically to slow it down.
Better: take a random sample, making it nondeterministic.
Better still: take the median of three random items.
05.09.2015

62

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Conclusions
Best case performance: O() or O()
Worst case performance: O(2 )
Average case performance: O()
Often we need the best "average case.
But sometimes we want a guarantee that we never have an awful
worst case.
We might even know we can rely on getting close to best case
situations and pick an algorithm based on that.
Know thy complexities http://bigocheatsheet.com/
05.09.2015

63

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Comparison

05.09.2015

64

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Comparison

05.09.2015

65

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Quick sort

05.09.2015

66

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Bubble sort

05.09.2015

67

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Merge sort

05.09.2015

68

Data Fusion between a 2D Laser Profile Sensor and a Camera

Sorting Algorithms
Insertion sort

05.09.2015

69

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 3
Examine the pseudocode below and describe the effect it has on the
given input sequence.
REARRANGE(A)
n <- length(A)
for j <- 0 to n
for i <- 0 to n-1
if (A[i] < A[i+1])
swap (A[i], A[i+1])

05.09.2015

70

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 3 Solution

Sequence becomes sorted from high to low.

05.09.2015

71

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 4
Use pseudocode to describe an algorithm that finds the smallest
number in a sequence.
Write a function in the programming language of your choice that
takes two sequences of numbers as parameters and returns a
sequence that contains the non-unique numbers from each input.
That is, those that are in the first and the second lists. There should
be no duplicates in the output. For example, given the sequences [1,
2, 3, 4, 5] and [4, 5, 6, 7], the answer is [4, 5].

05.09.2015

72

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 4 Solution

FIND SMALLEST(seq)
if size(seq)<1
raise error
smallest <- seq[0]
for all numbers i in seq
if i<smallest
smallest <- I
return smallest

05.09.2015

73

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 4 Solution

def non_unique(a,b):
result = []
for i in a:
if i in b and not i in result:
result.append(i)
return result

05.09.2015

74

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Linked Lists
A linked list is a data structure which also stores items in order,
accessed by index, without gaps.
The items have contiguous indices, but it does not use
contiguous memory - an element can be stored anywhere in
memory, regardless of where the element with the index one
higher or one lower is stored.
Each element stores a pointer to the next element - the "link.
Last element points to null.
First item is the "head.
Access the ith element by following i links from the head.

05.09.2015

75

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Double linked lists and circular lists
A doubly linked list also stores a pointer to the previous
element of the list.

A circular linked list stores a pointer from the last element


to the first element.

05.09.2015

76

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Accessing and changing content
To reach a given item, the list is traversed, beginning with the head.
Is this the item you want?
If not, follow the link and repeat.
Deleting an item is achieved by changing the link from the previous
item to point to the one after the one we want deleted.
Inserting is done by creating a new item and then changing the link
from the item we want it to follow to point to it, and making the new
item's link point to the item that previously followed the item that now
links to it.
These operations make more sense with a diagram and some notation.

05.09.2015

77

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Deletion

If your language doesn't perform automatic garbage collection,


you must now erase/delete the disconnected element.
05.09.2015

78

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Insertion

05.09.2015

79

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Efficiency
Linked lists have the opposite characteristics of an array.
Access to a particular element is slow because we have to work
our way through each element of the list to get there, its O(n).
However, insertion and deletion is fast (once we are at the element
we want to insert at) because we merely have to change which
element the pointer is pointing to, this is O(1).

05.09.2015

80

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Creating a linked list
We can consider a doubly linked list nodeN to have three attributes:
1. value[N] - the data item stored by the node.
2. previous[N] - a pointer to the previous node in the list.
3. next[N] a pointer to the next node in the list.
We can also define a list object, L, which contains two attributes:
1. head[L] - the first node in the list.
2. tail[L] - the last node in the list.
This is common pseudocode style notation.

05.09.2015

81

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Trees
A Tree is like a linked list, in that it has pointers to the 'next' node.
The difference, however, is that there may be more than one 'next
pointer.
In the case of a tree, we refer to the nodes pointed to by the
'next' pointers as either branches or children.
The node above a child is referred to as the parent node.
The topmost node is called the root.
Nodes without any children are called leaves.

05.09.2015

82

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Trees

05.09.2015

83

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Use for trees?
Some uses of trees:
1. Speed up navigation of data
2. Hierarchical data
3. Decision trees

05.09.2015

84

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Tree implementation
A tree node might be imagined diagrammatically like this:

With each child pointer pointing to another node object or NULL.


So far we have used simple data types that are their own key, but
if the data is complex, we might need something to refer to it by.
Imagine the value represents a whole employee record.
05.09.2015

85

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Difference from a linked list
The tree is somewhat like a linked list, in that it has nodes with
data and pointers to other nodes.
But the topology is different to a linked list, and has different
uses.
If a tree is well balanced, meaning that all the leaves are at
about the same height, arbitrary nodes can be reached in
logarithmic time.
This makes it faster to hunt down a value in a tree than a
linked list for many kinds of data.
05.09.2015

86

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Binary trees
While trees in general can have many children, a specific type
of tree is the binary tree.
A binary tree is a tree where each node has either 0, 1 or 2
children.
This has important implications for navigating a tree, as there
are at most two choices at any step

05.09.2015

87

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Node representation
A simple node for a tree with two children (or a binary tree)
could have the following attributes:
1. n.value - the value stored at the node (assuming we are
using simple values that are also keys).
2. n.parent - a pointer to the nodes parent.
3. n.left - a pointer to the nodes left child.
4. n.right - a pointer to the nodes right child.
The tree itself just needs a pointer to the root node.

05.09.2015

88

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Binary tree search
Used to quickly find data.
Given a tree with values inserted such that the left hand
values are always less than the right hand values, as below.

05.09.2015

89

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Inserting a node
BIN-TREE-INSERT(tree,item)
IF tree = tree=Node(item)
ELSE
IF tree.value > target
IF tree.left 0
tree.left=Node(item)
ELSE BIN-TREE-INSERT(tree.left,item)
ELSE
IF tree.right 0 tree.right=Node(item)
ELSE BIN-TREE-INSERT(tree.right,item)
return r
05.09.2015

90

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Searching for a node
BIN-TREE-FIND(tree, target)
r=tree
WHILE r
IF r.value = target
RETURN r
ELSE IF r.value > target
r=r.left
ELSE
r=r.right
RETURN
05.09.2015

91

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Efficiency
Can you see the between finding in a binary tree and using a binary
search?
Each check halves (roughly) the number of items left to search.
So, conversely, if we double the size of the tree, it takes how many more
steps? 1!
So, the relationship is (logN).
So how is this better than a list that we search with binary search?
Insertion into an array, in sorted order is O(N) (and the implementation is
slow anyway due to array movements).
And to use binary search we need a sorted sequence.
Insertion into a list is still O(N) because you have to find the right place.
Inserting into a binary tree is O(logN), so much faster.
05.09.2015

92

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Binary tree traversal
To traverse, or visit each node in a tree there are a number of
methods:
1. Preorder - output item, then follow left tree, then right tree.
2. Postorder - follow the left child, follow the right child, output
node value.
3. Breadth first - Start with the root and proceed in order of
increasing depth/height (i.e root, second level items, third
level items and so on).
4. In order - for each node, display the left hand side, then the
node itself, then the right. When displaying the left or right,
follow the same instructions.
05.09.2015

93

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Binary tree traversal
Preorder
10, 8, 5, 9, 14, 12, 11, 13, 17
Postorder
5, 9, 8, 11, 13, 12, 17, 14, 10
Breadth first
10, 8, 14, 5, 9, 12, 17, 11, 13
In order
5, 8, 9, 10, 11, 12, 13, 14, 17

05.09.2015

94

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Search

05.09.2015

95

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Traversal

05.09.2015

96

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 4

In a binary tree, what is the maximum and minimum number of


children a node may have?
Draw the binary tree resulting in the insertion of the numbers
15,4,7,10,5,2,3,1.

05.09.2015

97

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Graphs
Set of nodes and connections between them.

05.09.2015

98

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Graphs
Another way to represent data and relationships between them.
Used for:
Map analysis, route finding, path planning
Ranking search results
Analysing related data, such as social networks, proteins, etc.
Compiler optimisation
Timetabling
Physics simulations (actual physics)
Physics simulations (games)
Social connections: Facebook uses graphs for this
05.09.2015

99

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Graphs

05.09.2015

100

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Types of graphs
Undirected graph: A link is both ways.
Directed graph:
Each link is directional and does not imply the inverse
Vertex labelled graph:
The data used to identify each node is not the only data that is
important about that node.
For example, it might also have a "colour" value that affects
algorithms.
Cyclic graphs:
Has at least one "cycle". That is, a path from a node that leads
back to itself.
05.09.2015

101

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Types of graphs
Edge labelled graphs:
Links have data values that might affect algorithms.
Imagine in a graph of cities in which the links represent roads,
the length of the road might be recorded as the edge data.
Weighted graph:
The parameters along edges or at nodes are interval data and
can be summed and compared.
Directed acyclic graph:
Links have direction.
No cycles exist
Used a lot. Called a "DAG" for short.
05.09.2015

102

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Terminology

The nodes are called vertices.


The links are called edges (sometimes arcs).
An edge is incident to/on a vertex if it connects it to another.
Connected vertices are called adjacent or neighbours.
A vertex's degree is the number of edges incident on it.

05.09.2015

103

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Representing graphs
We can represent vertices with integers (actually, any unique value,
would do for the most part)Edges are pairs of vertices, (a,b),
connecting a and b.
A graph, G, has a set of vertices, V, and a set of edges, E.
We say G=(V,E).
We use n and m to represent the numbers of vertices and edges.
Think n for node if you find it hard to remember which way
around these are.

05.09.2015

104

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Visualisation
A graph G=(V,E) where:
V={1,2,3,4,5}
E={{1,2},{2,4},{3,4},{3,5},{4,5}}

05.09.2015

105

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Implementing graphs
There are two ways of implementing a graph when programming:
1. Adjacency Matrix: A two dimensional matrix of boolean values
where the value of a cell i, j is true if vertices i and j
are connected.
2. Adjacency List: Each vertex contains a list of nodes that it is
connected to.

05.09.2015

106

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Adjacency matrix
.

05.09.2015

107

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Adjacency list
.

05.09.2015

108

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Space complexity
So far, the data structures we have looked at have all taken about
. the same amount of memory space - O(), where n is the
number of elements.
The adjacency matrix requires O(2 ) space, where n is the
number of vertices.
Adjacency lists require O() space, where m is the number of
edges.
This method uses less space (generally - what if every node is
connected to every other?) and is faster to search for
common operations.
05.09.2015

109

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Weighted graph
.

05.09.2015

110

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Adjacency matrix for a weighted graph
.

05.09.2015

111

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Adjacency list for a weighted graph
.

05.09.2015

112

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Graph traversal
.

Storing data in a graph is fairly easy.


They don't really become useful until we can search for data or
find information about the interconnections.
Algorithms that interrogate the graph by following the edges are
called traversal algorithms.

05.09.2015

113

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Depth-First Search
.

Visit all nodes beginning with v in graph G.


Sometimes called Depth First Traversal (DFT).
Traverses a graph by visiting each vertex in turn.
Finds the first edge for the current vertex.
Follows it to reach the next unvisited vertex.
Marks the vertex as visited.
Repeats until all vertices are marked as visited.

05.09.2015

114

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Illustration
.
shttps://www.cs.usfca.edu/~galles/visualization/DFS.html
http://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.html

05.09.2015

115

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Illustration
.

05.09.2015

116

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Illustration
Output from out example graph is [1, 7, 5, 8, 6, 4, 3] when beginning
. with vertex 12 is never visited because no edges go to it.
Could have been different, but the edge ordering from each
node is arbitrary.
What other order could it be?

05.09.2015

117

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Breadth-First Search
. Traverses the graph by searching every edge from the current vertex.
Sometimes called Breadth First Traversal (BFT).
Only moves to the next vertex in the graph when all edges
have been explored.
The algorithm is the same as Depth First except that a Queue
is used to store the list of vertices to visit.
Will find the shortest path.

05.09.2015

118

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Breadth-First Search
.
http://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.html
https://www.cs.usfca.edu/~galles/visualization/BFS.html
http://joseph-harrington.com/2012/02/breadth-first-search-visual/

05.09.2015

119

Data Fusion between a 2D Laser Profile Sensor and a Camera

Linked Lists, Trees and Graphs


Breadth-First Search
Output from out example graph is [1, 5, 7, 8, 6, 4, 3] when beginning
. with vertex 12 is still never visited because no edges go to it.
Could have been different, but the edge ordering from each node
is arbitrary.
What other order could it be?

05.09.2015

120

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 5
Describe how a breadth first search (BFS) is performed on a graph
and state the order in which the nodes of the graph below will be
visited, beginning with A. Assume that visited nodes will be
remembered and not visited again, and that left-hand edges are
selected first.

05.09.2015

121

Data Fusion between a 2D Laser Profile Sensor and a Camera

Activity 5 Solution

ABCEFGDHIJ

05.09.2015

122

Data Fusion between a 2D Laser Profile Sensor and a Camera

Discussion

05.09.2015

123

You might also like