You are on page 1of 124

Programming, Algorithms

and Data Structures

Dr. Diana Hintea


Lecturer in Computer Science
Coventry University, UK

5/12/16

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Learning Outcomes
Object
Oriented
Programmi
ng
Concepts

Recursion

Linked
Lists, Trees
and Graphs
5/12/16

Algorithms
and their
Complexity
Sorting
Algorithms

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

Many languages support a method called object oriented


programming.
his method allows you to break a program up into objects.
Objects are pieces of code that consist of both functions and
Multiple functions and data items can be combined into an ob
Objects can also be reused.
Objects will often represent real objects that the program is
trying to represent e.g. employee, or student.

5/12/16

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

Classes and objects

hen you define an object you are really defining a class of ob


class acts like a template for an object.
class on its own does nothing.
ut once you have defined a class you can create many
nstances of it.
hese are called objects.
bjects are what do things and store data.

5/12/16

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

Main concepts

Encapsulatio
n
Inheritance
Polymorphis
m
Abstraction
5/12/16

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

Inheritance

class can use the member variables and methods of another


his is achieved through a mechanism called inheritance.
heritance can be thought of as specialising a general case in
more specific case.
or example, a car, or a bike, is a specialisation of a vehicle, w
s more general class.

5/12/16

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

ublic class Bicycle


public int gear;
public int speed;
public Bicycle(int startSpeed, int startGear)
{
gear = startGear;
speed = startSpeed;
}
5/12/16

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;
}
}
5/12/16

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;
}
}
5/12/16

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

Abstract class

A class which contains theabstractkeyword in its


declaration is known as abstract class.
Abstract classes may or may not containabstract
methodsie., methods with out body ( public void get();
)
But, if a class have at least one abstract method, then
the classmustbe 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.
If5/12/16
you inherit an abstract class you have to provide10

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();
}

5/12/16

11

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() { ... }
}
5/12/16

12

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.
5/12/16
13

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; }
}
5/12/16
14

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.

5/12/16

15

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Object Oriented
Programming Concepts

Polymorphism

ymorphism is when several classes have the same interface,


ten inherited from a base class. E.g. all shapes would have a
ea method, though the implementation is different for each s
ymorphism is the ability of an object to take on many forms.
e most common use of polymorphism in OOP occurs when a
ass reference is used to refer to a child class object.
y Java object that can pass more than one IS-A test is conside
be polymorphic.
5/12/16

16

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{
e Deer class is considered to be polymorphic since this has
ultiple 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
en we apply the reference variable facts to a Deer object ref
e following declarations are legal:
eer d = new Deer(); Animal a = d; Vegetarian v = d; Object o
5/12/16

17

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Algorithms and their Complexity


What is an algorithm?

A way of doing things.


Afinite list of instructions.
Aneffectivemethod.
Made ofwell-definedsteps.
A process of turning aninput stateinto anoutput state.
All of the above.

5/12/16

18

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 ofTuring Completenessand
Turing equivalencyto describe computers, processors an
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.

5/12/16

19

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.

5/12/16

20

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
5/12/16

21

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 m
programming languages which start at 0.

5/12/16

22

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
5/12/16
23

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

5/12/16

24

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Algorithms and their Complexity

5/12/16

25

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Algorithms and their Complexity

5/12/16

26

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Algorithms and their Complexity

5/12/16

27

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Algorithms and their Complexity

5/12/16

28

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 m
running the algorithm.
When using this notation, we are ignoring the actual run-time
of a measure of efficiency not dependent on a particular pla
We assume a single line of pseudo-code takes constant time
We also assume that the line which controls a loop takes con
though the loop itself may not.
Data types are assumed to have finite magnitude and precis
5/12/16

29

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 condition
this maybe on the first run, or it may have to search
data structure.
For analysis, we always consider the worst case.

5/12/16

30

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(): 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. 5/12/16
31

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

5/12/16

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

32

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
n3times, etc.

5/12/16

33

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)
5/12/16

for i list[1] .. list[length(list)-1]

34

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Algorithms and their Complexity


In more detail

The sum becomes1+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 out3n+2, but we would just write it in
big-O notation asO(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, we'd just write it asO().
5/12/16

35

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.

5/12/16

36

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Activity 1 Solution

5/12/16

, , [, ] ,

37

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 , , , , . State the function
for each plot.

5/12/16

38

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Activity 2 Solution

B
C
D
E

5/12/16

39

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 thebase case.
Example:is always true.
Every other case should take us toward the base case.
This is therecursive case
Example:for allx>1

5/12/16

40

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Recursion
Example

Factorial:
FACTORIAL(n)
if(n < 2)
return 1
else
return FACTORIAL(n-1) * n
The recursion stops when n is 1.

5/12/16

41

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Recursion
Example

5/12/16

42

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Recursion
Another example

Binary search:
http://www.cs.armstrong.edu/liang/animation/web/BinaryS
earch.html
BINARY_SEARCH(A, v, first, last)
if last < first
return false
i first + (last - first)/2
if A[i] == v

//Base case 1
//Base case

2
return true
if A[i] > v
BINARY_SEARCH(A,v,first,i-1)

//Recursion

case 1
5/12/16

else

43

Data Fusion between a 2D Laser Profile Sensor and a


Camera

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

5/12/16

44

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.
5/12/16
45

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 crazyO((n+1)!)
Worst case isforever.

5/12/16

46

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Bogosort

5/12/16

47

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.

5/12/16

48

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Insertion sort

5/12/16

49

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()
Average case performance: O()
5/12/16

50

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.

5/12/16

51

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Bubble sort

5/12/16

52

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()
Average case performance: O()
5/12/16

53

Data Fusion between a 2D Laser Profile Sensor and a


Camera

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

5/12/16

54

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Merge sort

5/12/16

55

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)

5/12/16

56

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++
5/12/16

57

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 clearlyO(n)(wherenis 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
wasO(logn)still applies, but now it's used to show
that we apply the mergelogntimes and so we
haveO(nlogn).
5/12/16

58

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 hasnelements that must be processed.
There arelognlayers to process.
So, processnitemslogntimes

5/12/16

59

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).

5/12/16

60

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Quick sort

5/12/16

61

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(
5/12/16

62

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 itissorted? If it's sorted, then we
end up withO(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
5/12/16
63
still: take the median of three random items.

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Conclusions

Best case performance: O() or O()


Worst case performance: O()
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 complexitieshttp://bigocheatsheet.com/
5/12/16

64

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Comparison

5/12/16

65

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Comparison

5/12/16

66

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Quick sort

5/12/16

67

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Bubble sort

5/12/16

68

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Merge sort

5/12/16

69

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Sorting Algorithms
Insertion sort

5/12/16

70

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])

5/12/16

71

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Activity 3 Solution

Sequence becomes sorted from high to low.

5/12/16

72

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].

5/12/16

73

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

5/12/16

74

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

5/12/16

75

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 theithelement by followingilinks from the head.

5/12/16

76

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 elem


to the first element.

5/12/16

77

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 oneafterthe 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.
5/12/16
78
These
operations make more sense with a diagram and

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Deletion

your language doesn't perform automatic garbage collection


you must now erase/delete the disconnected element.
5/12/16

79

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Insertion

5/12/16

80

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
our way through each element of the list to get there, i
However, insertion and deletion is fast (once we are at t
we want to insert at) because we merely have to chang
element the pointer is pointing to, this isO(1).

5/12/16

81

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 thre


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

5/12/16

82

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 'n


The difference, however, is that there may be more than
pointer.
In the case of a tree, we refer to the nodes pointed to by
'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.

5/12/16

83

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Trees

5/12/16

84

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

5/12/16

85

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 objec


So far we have used simple data types that are their o
if the data is complex, we might need something to r
Imagine the value represents a whole employee recor
5/12/16

86

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.
5/12/16

87

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 thebinary 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

5/12/16

88

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.
5/12/16

89

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.

5/12/16

90

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Inserting a node

5/12/16

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-TREEINSERT(tree.left,item)
ELSE
IF tree.right 0
tree.right=Node(item)
ELSE
BIN-TREE-

91

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
5/12/16

92

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 wedoublethe 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 isO(N)(and the
implementation is slow anyway due to array movements).
And to use binary search weneeda sorted sequence.
Insertion into a list is stillO(N)because you have to find
5/12/16
93
the right place.

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
5/12/16

94

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

5/12/16

95

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Search

5/12/16

96

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Traversal

5/12/16

97

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.

5/12/16

98

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Graphs
Set of nodes and connections
between them.

5/12/16

99

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)
100
5/12/16
Social connections: Facebook uses graphs for this

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Graphs

5/12/16

101

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 invers
Vertex labelled graph:
The data used to identify each node is not the only d
important about that node.
For example, it might also have a "colour" value that
algorithms.
Cyclic graphs:
Has at least one "cycle". That is, a path from a node
back to itself.
5/12/16

102

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 represe
the length of the road might be recorded as the edg
Weighted graph:
The parameters along edges or at nodes are interval
can be summed and compared.
Directed acyclic graph:
Links have direction.
No cycles exist
Used a lot. Called a "DAG" for short.
5/12/16

103

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Terminology

The nodes are calledvertices.


The links are callededges(sometimesarcs).
An edge isincidentto/on a vertex if it connects it to an
Connected vertices are calledadjacentorneighbours
A vertex'sdegreeis the number of edges incident on it

5/12/16

104

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 u


would do for the most part)Edges are pairs of vertices,
connectingaandb.
A graph,G, has a set of vertices,V, and a set of edges,
We sayG=(V,E).
We usenandmto represent the numbers of vertices an
Thinknfor node if you find it hard to remember which w
around these are.

5/12/16

105

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


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

5/12/16

106

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 prog


1. Adjacency Matrix: A two dimensional matrix of bo
where the value of a celli,jis true if verticesiand
are connected.
2. Adjacency List: Each vertex contains a list of nod
connected to.

5/12/16

107

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Adjacency matrix
.

5/12/16

108

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Adjacency list
.

5/12/16

109

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 t


.
the same amount of memory space- O(),wherenis t
number of elements.
The adjacency matrix requiresO()space, wherenis th
number of vertices.
Adjacency lists requireO()space, wheremis the numb
edges.
This method uses less space (generally- what if ever
connected to every other?) and is faster to search for
common operations.
5/12/16

110

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Weighted graph
.

5/12/16

111

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Adjacency matrix for a weighted
graph
.

5/12/16

112

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Adjacency list for a weighted
graph
.

5/12/16

113

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 fo
find information about the interconnections.
Algorithms that interrogate the graph by following the
calledtraversal algorithms.

5/12/16

114

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.

5/12/16

115

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.htm
http://www.comp.nus.edu.sg/~stevenha/visualization/df

5/12/16

116

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Linked Lists, Trees and Graphs


Illustration
.

5/12/16

117

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] wh


. with vertex 12 is never visited because no edges goto
Could have been different, but the edge ordering from e
node is arbitrary.
What other order could it be?

5/12/16

118

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 c


Sometimes called Breadth First Traversal (BFT).
Only moves to the next vertex in the graph when all ed
have been explored.
The algorithm is the same as Depth First except that a Q
is used to store the list of vertices to visit.
Will find the shortest path.

5/12/16

119

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/df
https://www.cs.usfca.edu/~galles/visualization/BFS.htm
http://joseph-harrington.com/2012/02/breadth-first-sear

5/12/16

120

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] wh


. with vertex 12 isstillnever visited because no edges
Could have been different, but the edge ordering from e
is arbitrary.
What other order could it be?

5/12/16

121

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.

5/12/16

122

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Activity 5 Solution

ABCEFGDHIJ

5/12/16

123

Data Fusion between a 2D Laser Profile Sensor and a


Camera

Discussion

5/12/16

124

You might also like