Lecture 210 Ct

Embed Size (px)

Citation preview

  • 8/17/2019 Lecture 210 Ct

    1/124

     

    Programming, Algorithms

    and Data Structures 

    Dr. Diana HinteaLecturer in Computer Science

    Coventry University, UK

    05.09.2015 

  • 8/17/2019 Lecture 210 Ct

    2/124

    Object Oriented

    Programming

    Concepts

    Learning Outcomes

    05.09.2015 1

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

     

    Recursion

    Linked Lists,

    Trees and Graphs

    Algorithms

    and their

    Complexity

    Sorting

    Algorithms 

  • 8/17/2019 Lecture 210 Ct

    3/124

    Object Oriented

    Programming Concepts

    05.09.2015 2

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

     

    • 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 istrying to represent e.g. employee, or student.

  • 8/17/2019 Lecture 210 Ct

    4/124

    Object Oriented

    Programming Concepts

    05.09.2015 3

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

     

    • 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 it’s 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.

    Classes and objects

  • 8/17/2019 Lecture 210 Ct

    5/124

    Object Oriented

    Programming Concepts

    05.09.2015 4

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

     

    Encapsulation

    Inheritance

    Polymorphism

    Abstraction

    Main concepts

  • 8/17/2019 Lecture 210 Ct

    6/124

    Object Oriented

    Programming Concepts

    05.09.2015 5

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

     

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

    Inheritance

  • 8/17/2019 Lecture 210 Ct

    7/124

    Object Oriented

    Programming Concepts

    05.09.2015 6

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

     

    public class Bicycle

    {

    public int gear;public int speed;

    public Bicycle(int startSpeed, int startGear)

    {

    gear = startGear;

    speed = startSpeed;

    }

  • 8/17/2019 Lecture 210 Ct

    8/124

    Object Oriented

    Programming Concepts

    05.09.2015 7

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

     

    public void setGear(int newValue)

    {

    gear = newValue;

    }public void applyBrake(int decrement)

    {

    speed -= decrement;

    }public void speedUp(int increment)

    {

    speed += increment;

    } }

  • 8/17/2019 Lecture 210 Ct

    9/124

    Object Oriented

    Programming Concepts

    05.09.2015 8

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

     

    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;

    }

    }

  • 8/17/2019 Lecture 210 Ct

    10/124

    Object Oriented

    Programming Concepts

    05.09.2015 9

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

     

    • A class which contains the abstract keyword in its declaration is

    known as abstract class.

    • Abstract classes may or may not contain abstractmethods ie.,methods with out body ( public void get(); )

    • But, if a class have at least one abstract method, then the

    classmust

     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.

    Abstract class

  • 8/17/2019 Lecture 210 Ct

    11/124

    Object Oriented

    Programming Concepts

    05.09.2015 10

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

     

    abstract class GraphicObject{

    int x, y;

    void moveTo(int newX, int newY) { ... }

    abstract void draw();

    abstract void resize();}

    Abstract class

  • 8/17/2019 Lecture 210 Ct

    12/124

    Object Oriented

    Programming Concepts

    05.09.2015 11

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

     

    class Circle extends GraphicObject

    {

    void draw() { ... }

    void resize() { ... }

    }

    class Rectangle extends GraphicObject{

    void draw() { ... }

    void resize() { ... }

    }

    Abstract class

  • 8/17/2019 Lecture 210 Ct

    13/124

    Object Oriented

    Programming Concepts

    05.09.2015 12

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

     

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

    Encapsulation

  • 8/17/2019 Lecture 210 Ct

    14/124

    Object Oriented

    Programming Concepts

    05.09.2015 13

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

     

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

    }

  • 8/17/2019 Lecture 210 Ct

    15/124

    Object Oriented

    Programming Concepts

    05.09.2015 14

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

     

    The public setX() and getX() methods are the access points of theinstance 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.

  • 8/17/2019 Lecture 210 Ct

    16/124

    Object Oriented

    Programming Concepts

    05.09.2015 15

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

     

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

    Polymorphism

  • 8/17/2019 Lecture 210 Ct

    17/124

    Object Oriented

    Programming Concepts

    05.09.2015 16

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

     

    public interface Vegetarian{}

    public class Animal{}

    public class Deer extends Animal implements Vegetarian{}

    • The Deer class is considered to be polymorphic since this hasmultiple 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;

  • 8/17/2019 Lecture 210 Ct

    18/124

    Algorithms and their Complexity

    05.09.2015 17

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

     

    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.

  • 8/17/2019 Lecture 210 Ct

    19/124

    Algorithms and their Complexity

    05.09.2015 18

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

     

    What do we need to express them?

    • Almost anything.

    • We use the ideas of Turing Completeness andTuring 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.

  • 8/17/2019 Lecture 210 Ct

    20/124

    Algorithms and their Complexity

    05.09.2015 19

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

     

    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.

  • 8/17/2019 Lecture 210 Ct

    21/124

    Algorithms and their Complexity

    05.09.2015 20

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

     

    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]

     j ← iwhile j > 0 and A [j - 1] > key

    A [j] ← A [j - 1]

     j ← j - 1

    A[j] ← key 

  • 8/17/2019 Lecture 210 Ct

    22/124

    Algorithms and their Complexity

    05.09.2015 21

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

     

    A few rules

    1. Assignment

    • Pseudo-code uses ← for assignment instead of ‘=‘

    • Sometimes written as

  • 8/17/2019 Lecture 210 Ct

    23/124

    Algorithms and their Complexity

    05.09.2015 22

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

     

    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)

  • 8/17/2019 Lecture 210 Ct

    24/124

    Algorithms and their Complexity

    05.09.2015 23

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

     

    Nice video on algorithms

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

  • 8/17/2019 Lecture 210 Ct

    25/124

    Algorithms and their Complexity

    05.09.2015 24

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

     

  • 8/17/2019 Lecture 210 Ct

    26/124

    Algorithms and their Complexity

    05.09.2015 25

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

     

  • 8/17/2019 Lecture 210 Ct

    27/124

    Algorithms and their Complexity

    05.09.2015 26

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

     

  • 8/17/2019 Lecture 210 Ct

    28/124

    Algorithms and their Complexity

    05.09.2015 27

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

     

  • 8/17/2019 Lecture 210 Ct

    29/124

    Algorithms and their Complexity

    05.09.2015 28

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

     

    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.

  • 8/17/2019 Lecture 210 Ct

    30/124

    Algorithms and their Complexity

    05.09.2015 29

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

     

    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.

  • 8/17/2019 Lecture 210 Ct

    31/124

    Algorithms and their Complexity

    05.09.2015 30

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

     

    The notation

    • O(): This represents an algorithm whose performance will drop

    linearly with the size of the input. For example, with an input sizeof 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.

    • O(log()): This represents an algorithm whose performance will

    drop as the log of the input.

  • 8/17/2019 Lecture 210 Ct

    32/124

    Algorithms and their Complexity

    05.09.2015 31

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

     

    Example

    INSERTION-SORT(A)

    for j ← 1 to length[A] (n times) key ← A[j]

    (n times)

    i←j-1(n times)

    while i > 0 and A[i] > key(n*n times)

    A[i+1] ← A[i] (n*n times)

    i←i+1(n*n times)

     

    A[i+1] ← key(n times)

  • 8/17/2019 Lecture 210 Ct

    33/124

    Algorithms and their Complexity

    05.09.2015 32

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

     

    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.

  • 8/17/2019 Lecture 210 Ct

    34/124

    Algorithms and their Complexity

    05.09.2015 33

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

     

    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)

  • 8/17/2019 Lecture 210 Ct

    35/124

    Algorithms and their Complexity

    05.09.2015 34

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

     

    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 ofthe time, that’s 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 + , we'd just write it as O().

  • 8/17/2019 Lecture 210 Ct

    36/124

    05.09.2015 35

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

     

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

    •   4 

    •    

    •   +

    •   . 

    •   3log()

    •   40 

    •   log()

    Activity 1

  • 8/17/2019 Lecture 210 Ct

    37/124

    05.09.2015 36

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

     

     , 4

    , .

    , 3log(), [ + , 40] , log() 

    Activity 1 Solution

  • 8/17/2019 Lecture 210 Ct

    38/124

    05.09.2015 37

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

     

    • Examine the graph below. The plots A, B, C, D, E represent the growth

    rates log(), ,  , 20, 3. State the function for each plot.

    Activity 2

  • 8/17/2019 Lecture 210 Ct

    39/124

    05.09.2015 38

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

     

    A  

    B 3 C 20 

    D log()

    E  

    Activity 2 Solution

  • 8/17/2019 Lecture 210 Ct

    40/124

    Recursion

    05.09.2015 39

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

     

    • A recursive function calls itself.

    It must have a condition to stop this process.• This is the base case. 

    • Example: = 1 is always true.

    • Every other case should take us toward the base case.

    • This is the recursive case 

    • Example: = − ×  for all x>1

    What is recursion?

  • 8/17/2019 Lecture 210 Ct

    41/124

    Recursion

    05.09.2015 40

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

     

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

    Example

  • 8/17/2019 Lecture 210 Ct

    42/124

    Recursion

    05.09.2015 41

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

     

    Example

  • 8/17/2019 Lecture 210 Ct

    43/124

    Recursion

    05.09.2015 42

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

     

    • Binary search:

    http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html BINARY_SEARCH(A, v, first, last)

    if last < first //Base case 1return false

    i ← first + (last - first)/2

    if A[i] == v //Base case 2

    return true

    if A[i] > vBINARY_SEARCH(A,v,first,i-1) //Recursion case 1

    else

    BINARY_SEARCH(A,v,i+1,last) //Recursion case 2 

    Another example

    http://www.cs.armstrong.edu/liang/animation/web/BinarySearch.htmlhttp://www.cs.armstrong.edu/liang/animation/web/BinarySearch.html

  • 8/17/2019 Lecture 210 Ct

    44/124

    Recursion

    05.09.2015 43

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

     

    • Towers of Hanoi:

    http://www.dynamicdrive.com/dynamicindex12/towerhanoi.htm

    Another example

  • 8/17/2019 Lecture 210 Ct

    45/124

    Sorting Algorithms

    05.09.2015 44

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

     

    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.

  • 8/17/2019 Lecture 210 Ct

    46/124

    Sorting Algorithms

    05.09.2015 45

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

     

    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 is……forever.

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

     

  • 8/17/2019 Lecture 210 Ct

    47/124

    Sorting Algorithms

    05.09.2015 46

    Bogosort

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

     

  • 8/17/2019 Lecture 210 Ct

    48/124

    Sorting Algorithms

    05.09.2015 47

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    49/124

    Sorting Algorithms

    05.09.2015 48

    Insertion sort

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

     

  • 8/17/2019 Lecture 210 Ct

    50/124

    Sorting Algorithms

    05.09.2015 49

    Insertion sort

    INSERTION-SORT(A)

    for i ← 1 to length[A]

    key ← A[i]

     j←i 

    while j > 0 and A[j-1] > key

    A[j] ← A[j-1]

     j←j-1

    A[j] ← key• Best case performance: O()

    • Worst case performance: O()

    • Average case performance: O()

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

     

  • 8/17/2019 Lecture 210 Ct

    51/124

    Sorting Algorithms

    05.09.2015 50

    Bubble sort

    • Examine each consecutive pair of items.• If they are out of order, swap them.

    • Repeat until no swaps are made.

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

     

  • 8/17/2019 Lecture 210 Ct

    52/124

    Sorting Algorithms

    05.09.2015 51

    Bubble sort

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

     

  • 8/17/2019 Lecture 210 Ct

    53/124

    Sorting Algorithms

    05.09.2015 52

    Bubble sort

    BUBBLE(A)

    swap←True 

    while swap is True:swap←False 

    for i ← 0 to length(A)-1

    if A[i] > A[i+1]

    swap←True 

    swap(A[i],A[i+1])• Best case performance: O()

    • Worst case performance: O()

    • Average case performance: O()

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

     

  • 8/17/2019 Lecture 210 Ct

    54/124

    Sorting Algorithms

    05.09.2015 53

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    55/124

    Sorting Algorithms

    05.09.2015 54

    Merge sort

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

     

  • 8/17/2019 Lecture 210 Ct

    56/124

    Sorting Algorithms

    05.09.2015 55

    Merge sort

    MERGE-SORT(l):

    if length(l)

  • 8/17/2019 Lecture 210 Ct

    57/124

    Sorting Algorithms

    05.09.2015 56

    Merge sort

    MERGE(a,b)

    out ← array of length length(a)+length(b) 

    i←0  j←0 

    for x ← 0 length(out) 

    if a[i] < b[j]

    out[x] = a[i]

    i++else

    out[x] = b[j]

     j++

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

     

  • 8/17/2019 Lecture 210 Ct

    58/124

    Sorting Algorithms

    05.09.2015 57

    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 searchwas O(logn) still applies, but now it's used to show that we apply

    the merge logn times and so we have O(nlogn).

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

     

  • 8/17/2019 Lecture 210 Ct

    59/124

    Sorting Algorithms

    05.09.2015 58

    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

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

     

  • 8/17/2019 Lecture 210 Ct

    60/124

    Sorting Algorithms

    05.09.2015 59

    Quick sort

    • Pick a "pivot“. 

    • Create two empty lists, one for all items less than the pivot, onefor 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).

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

     

  • 8/17/2019 Lecture 210 Ct

    61/124

    Sorting Algorithms

    05.09.2015 60

    Quick sort

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

     

  • 8/17/2019 Lecture 210 Ct

    62/124

    Sorting Algorithms

    05.09.2015 61

    Quick sort

    function QUICKSORT(A)

    if length(A)

  • 8/17/2019 Lecture 210 Ct

    63/124

    Sorting Algorithms

    05.09.2015 62

    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'tmatter, 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.

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

     

  • 8/17/2019 Lecture 210 Ct

    64/124

    Sorting Algorithms

    05.09.2015 63

    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 casesituations and pick an algorithm based on that.

    • Know thy complexities http://bigocheatsheet.com/ 

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

     

    http://bigocheatsheet.com/http://bigocheatsheet.com/http://bigocheatsheet.com/

  • 8/17/2019 Lecture 210 Ct

    65/124

    Sorting Algorithms

    05.09.2015 64

    Comparison

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

     

  • 8/17/2019 Lecture 210 Ct

    66/124

    Sorting Algorithms

    05.09.2015 65

    Comparison

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

     

  • 8/17/2019 Lecture 210 Ct

    67/124

    Sorting Algorithms

    05.09.2015 66

    Quick sort

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

     

  • 8/17/2019 Lecture 210 Ct

    68/124

    Sorting Algorithms

    05.09.2015 67

    Bubble sort

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

     

  • 8/17/2019 Lecture 210 Ct

    69/124

    Sorting Algorithms

    05.09.2015 68

    Merge sort

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

     

  • 8/17/2019 Lecture 210 Ct

    70/124

    Sorting Algorithms

    05.09.2015 69

    Insertion sort

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

     

  • 8/17/2019 Lecture 210 Ct

    71/124

    05.09.2015 70

    • Examine the pseudocode below and describe the effect it has on the

    given input sequence.

    REARRANGE(A)n

  • 8/17/2019 Lecture 210 Ct

    72/124

    05.09.2015 71

    Sequence becomes sorted from high to low.

    Activity 3 Solution

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

     

  • 8/17/2019 Lecture 210 Ct

    73/124

    05.09.2015 72

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

    Activity 4

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

     

  • 8/17/2019 Lecture 210 Ct

    74/124

    05.09.2015 73

    FIND SMALLEST(seq)

    if size(seq)

  • 8/17/2019 Lecture 210 Ct

    75/124

    05.09.2015 74

    def non_unique(a,b):

    result = []

    for i in a:

    if i in b and not i in result:

    result.append(i)

    return result

    Activity 4 Solution

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

     

  • 8/17/2019 Lecture 210 Ct

    76/124

    Linked Lists, Trees and Graphs

    05.09.2015 75

    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 inmemory, 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.

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

     

  • 8/17/2019 Lecture 210 Ct

    77/124

    Linked Lists, Trees and Graphs

    05.09.2015 76

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    78/124

    Linked Lists, Trees and Graphs

    05.09.2015 77

    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 nowlinks to it.

    • These operations make more sense with a diagram and some notation.

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

     

  • 8/17/2019 Lecture 210 Ct

    79/124

    Linked Lists, Trees and Graphs

    05.09.2015 78

    Deletion

    • If your language doesn't perform automatic garbage collection,

    you must now erase/delete the disconnected element.

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

     

  • 8/17/2019 Lecture 210 Ct

    80/124

    Linked Lists, Trees and Graphs

    05.09.2015 79

    Insertion

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

     

  • 8/17/2019 Lecture 210 Ct

    81/124

    Linked Lists, Trees and Graphs

    05.09.2015 80

    Efficiency

    • Linked lists have the opposite characteristics of an array.

    • Access to a particular element is slow because we have to workour way through each element of the list to get there, it’s 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).

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

     

  • 8/17/2019 Lecture 210 Ct

    82/124

    Linked Lists, Trees and Graphs

    05.09.2015 81

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    83/124

    Linked Lists, Trees and Graphs

    05.09.2015 82

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    84/124

    Linked Lists, Trees and Graphs

    05.09.2015 83

    Trees

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

     

  • 8/17/2019 Lecture 210 Ct

    85/124

    Linked Lists, Trees and Graphs

    05.09.2015 84

    Use for trees?

    • Some uses of trees:

    1. Speed up navigation of data2. Hierarchical data

    3. Decision trees

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

     

  • 8/17/2019 Lecture 210 Ct

    86/124

    Linked Lists, Trees and Graphs

    05.09.2015 85

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    87/124

    Linked Lists, Trees and Graphs

    05.09.2015 86

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    88/124

    Linked Lists, Trees and Graphs

    05.09.2015 87

    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

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

     

  • 8/17/2019 Lecture 210 Ct

    89/124

    Linked Lists, Trees and Graphs

    05.09.2015 88

    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 node’s parent.

    3. n.left -

    a pointer to the node’s left child.

    4. n.right - a pointer to the node’s right child.• The tree itself just needs a pointer to the root node.

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

     

  • 8/17/2019 Lecture 210 Ct

    90/124

    Linked Lists, Trees and Graphs

    05.09.2015 89

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    91/124

    Linked Lists, Trees and Graphs

    05.09.2015 90

    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)

    ELSEIF tree.right ≠ 0 tree.right=Node(item)

    ELSE BIN-TREE-INSERT(tree.right,item)

    return r

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

     

  • 8/17/2019 Lecture 210 Ct

    92/124

    Linked Lists, Trees and Graphs

    05.09.2015 91

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

    r=r.right

    RETURN Ø

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

     

  • 8/17/2019 Lecture 210 Ct

    93/124

    Linked Lists, Trees and Graphs

    05.09.2015 92

    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 moresteps? 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.

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

     

  • 8/17/2019 Lecture 210 Ct

    94/124

    Linked Lists, Trees and Graphs

    05.09.2015 93

    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, outputnode 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.

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

     

  • 8/17/2019 Lecture 210 Ct

    95/124

    Linked Lists, Trees and Graphs

    05.09.2015 94

    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

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

     

  • 8/17/2019 Lecture 210 Ct

    96/124

    Linked Lists, Trees and Graphs

    05.09.2015 95

    Search

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

     

  • 8/17/2019 Lecture 210 Ct

    97/124

    Linked Lists, Trees and Graphs

    05.09.2015 96

    Traversal

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

     

  • 8/17/2019 Lecture 210 Ct

    98/124

    05.09.2015 97

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

    Activity 4

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

     

  • 8/17/2019 Lecture 210 Ct

    99/124

    Linked Lists, Trees and Graphs

    05.09.2015 98

    Graphs

    • Set of nodes and connections between them.

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

     

  • 8/17/2019 Lecture 210 Ct

    100/124

    Linked Lists, Trees and Graphs

    05.09.2015 99

    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

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

     

  • 8/17/2019 Lecture 210 Ct

    101/124

    Linked Lists, Trees and Graphs

    05.09.2015 100

    Graphs

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

     

  • 8/17/2019 Lecture 210 Ct

    102/124

    Linked Lists, Trees and Graphs

    05.09.2015 101

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    103/124

    Linked Lists, Trees and Graphs

    05.09.2015 102

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    104/124

    Linked Lists, Trees and Graphs

    05.09.2015 103

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    105/124

    Linked Lists, Trees and Graphs

    05.09.2015 104

    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 wayaround these are.

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

     

  • 8/17/2019 Lecture 210 Ct

    106/124

    Linked Lists, Trees and Graphs

    05.09.2015 105

    Visualisation

    • A graph G=(V,E) where:

    • V={1,2,3,4,5}

    • E={{1,2},{2,4},{3,4},{3,5},{4,5}}

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

     

  • 8/17/2019 Lecture 210 Ct

    107/124

    Linked Lists, Trees and Graphs

    05.09.2015 106

    Implementing graphs

    • There are two ways of implementing a graph when programming:

    1. Adjacency Matrix: A two dimensional matrix of boolean valueswhere 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.

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

     

  • 8/17/2019 Lecture 210 Ct

    108/124

    Linked Lists, Trees and Graphs

    05.09.2015 107

    Adjacency matrix

    .

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

     

  • 8/17/2019 Lecture 210 Ct

    109/124

    Linked Lists, Trees and Graphs

    05.09.2015 108

    Adjacency list

    .

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

     

  • 8/17/2019 Lecture 210 Ct

    110/124

    Linked Lists, Trees and Graphs

    05.09.2015 109

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

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

     

  • 8/17/2019 Lecture 210 Ct

    111/124

    Linked Lists, Trees and Graphs

    05.09.2015 110

    Weighted graph

    .

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

     

  • 8/17/2019 Lecture 210 Ct

    112/124

    Linked Lists, Trees and Graphs

    05.09.2015 111

    Adjacency matrix for a weighted graph

    .

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

     

  • 8/17/2019 Lecture 210 Ct

    113/124

    Linked Lists, Trees and Graphs

    05.09.2015 112

    Adjacency list for a weighted graph

    .

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

     

  • 8/17/2019 Lecture 210 Ct

    114/124

    Linked Lists, Trees and Graphs

    05.09.2015 113

    Graph traversal

    .• Storing data in a graph is fairly easy.

    • They don't really become useful until we can search for data orfind information about the interconnections.

    • Algorithms that interrogate the graph by following the edges are

    called traversal algorithms. 

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

     

  • 8/17/2019 Lecture 210 Ct

    115/124

    Linked Lists, Trees and Graphs

    05.09.2015 114

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    116/124

    Linked Lists, Trees and Graphs

    05.09.2015 115

    Illustration

    .• shttps://www.cs.usfca.edu/~galles/visualization/DFS.html 

    • http://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.html 

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

     

    https://www.cs.usfca.edu/~galles/visualization/DFS.htmlhttp://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.htmlhttp://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.htmlhttp://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.htmlhttp://www.comp.nus.edu.sg/~stevenha/visualization/dfsbfs.htmlhttps://www.cs.usfca.edu/~galles/visualization/DFS.htmlhttps://www.cs.usfca.edu/~galles/visualization/DFS.htmlhttps://www.cs.usfca.edu/~galles/visualization/DFS.htmlhttps://www.cs.usfca.edu/~galles/visualization/DFS.html

  • 8/17/2019 Lecture 210 Ct

    117/124

    Linked Lists, Trees and Graphs

    05.09.2015 116

    Illustration

    .

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

     

  • 8/17/2019 Lecture 210 Ct

    118/124

    Linked Lists, Trees and Graphs

    05.09.2015 117

    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 goto

     it.

    • Could have been different, but the edge ordering from each

    node is arbitrary.

    • What other order could it be?

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

     

  • 8/17/2019 Lecture 210 Ct

    119/124

    Linked Lists, Trees and Graphs

    05.09.2015 118

    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.

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

     

  • 8/17/2019 Lecture 210 Ct

    120/124

    Linked Lists, Trees and Graphs

    05.09.2015 119

    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/ 

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

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

  • 8/17/2019 Lecture 210 Ct

    121/124

    Linked Lists, Trees and Graphs

    05.09.2015 120

    Breadth-First Search

    .

    • Output from out example graph is [1, 5, 7, 8, 6, 4, 3] when beginning

    with vertex 12 isstill

     never visited because no edges goto

     it.

    • Could have been different, but the edge ordering from each node

    is arbitrary.

    • What other order could it be?

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

  • 8/17/2019 Lecture 210 Ct

    122/124

    05.09.2015 121

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

    Activity 5

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

  • 8/17/2019 Lecture 210 Ct

    123/124

    05.09.2015 122

    A B C E F G D H I J

    Activity 5 Solution

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

  • 8/17/2019 Lecture 210 Ct

    124/124

    Discussion

    … 

    … 

    …