180
Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms Overview Long numbers Gallery of sorting algorithms Median in a linear time Graphs and their representation, Graph-algorithms.

Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Overview

Long numbers

Gallery of sorting algorithms

Median in a linear time

Graphs and their representation,

Graph-algorithms.

Page 2: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Long numbersare when

we have a number that does not fit into any numericaldata-type.

Page 3: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Long numbersare when

we have a number that does not fit into any numericaldata-type.

We have to implement it ourselves.

Page 4: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Long numbersare when

we have a number that does not fit into any numericaldata-type.

We have to implement it ourselves.

We may use arrays (where possible), or dynamicdata-structures.

Page 5: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Long numbersare when

we have a number that does not fit into any numericaldata-type.

We have to implement it ourselves.

We may use arrays (where possible), or dynamicdata-structures.

We want to implement mainly addition, subtraction,multiplication and division.

Page 6: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Long numbersare when

we have a number that does not fit into any numericaldata-type.

We have to implement it ourselves.

We may use arrays (where possible), or dynamicdata-structures.

We want to implement mainly addition, subtraction,multiplication and division.

Problems with non-integer numbers and with negativenumbers!

Page 7: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing long numbers

We focus on dynamic structures.

Page 8: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing long numbers

We focus on dynamic structures.

We can use a bidirectional list.

Page 9: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing long numbers

We focus on dynamic structures.

We can use a bidirectional list.

With one-directional list we would have to decide on itsendian-ness!

Page 10: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Details

Shown on the black-board.

Page 11: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heap

A heap is a tree where for each vertex (node) it holds that thevalue of the parent is at most as large as the value of (all)sons.

Page 12: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heap

A heap is a tree where for each vertex (node) it holds that thevalue of the parent is at most as large as the value of (all)sons.A binary leftist heap is a binary tree that fulfils theheap-property in such that in all the levels except of thebottom-most two all vertices have two sons. In the bottommost level no vertces hav any sons. And in the second-to-lastlevel, there exists at most one entry with exactly one son, andthis son is to the left. Moreover, all the vertices to the leftfrom this entry have two sons, and the vertices to the righthave none.

Page 13: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heap

A heap is a tree where for each vertex (node) it holds that thevalue of the parent is at most as large as the value of (all)sons.A binary leftist heap is a binary tree that fulfils theheap-property in such that in all the levels except of thebottom-most two all vertices have two sons. In the bottommost level no vertces hav any sons. And in the second-to-lastlevel, there exists at most one entry with exactly one son, andthis son is to the left. Moreover, all the vertices to the leftfrom this entry have two sons, and the vertices to the righthave none.Idea: The heap is being filled from the top and the depthincreases only if all the previous levels are already full. Thebottom-most level is filled ”from the left to the right”.

Page 14: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heap

A heap is a tree where for each vertex (node) it holds that thevalue of the parent is at most as large as the value of (all)sons.A binary leftist heap is a binary tree that fulfils theheap-property in such that in all the levels except of thebottom-most two all vertices have two sons. In the bottommost level no vertces hav any sons. And in the second-to-lastlevel, there exists at most one entry with exactly one son, andthis son is to the left. Moreover, all the vertices to the leftfrom this entry have two sons, and the vertices to the righthave none.Idea: The heap is being filled from the top and the depthincreases only if all the previous levels are already full. Thebottom-most level is filled ”from the left to the right”.With a heap we use the operations makeheap andextractmin.

Page 15: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heapsort

Heapsort can be described very easily:

Page 16: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heapsort

Heapsort can be described very easily:

H:=Makeheap;

Page 17: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heapsort

Heapsort can be described very easily:

H:=Makeheap;

for i:=1 to n do output(extractmin(H))

Page 18: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Heapsort

Heapsort can be described very easily:

H:=Makeheap;

for i:=1 to n do output(extractmin(H))

Now, how can one implement those functions?

Page 19: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing Makeheap

Naive approach: Insert one element after another.

Page 20: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing Makeheap

Naive approach: Insert one element after another.

Use function bubble upwards for heap-consolidation.

Page 21: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing Makeheap

Naive approach: Insert one element after another.

Use function bubble upwards for heap-consolidation.

Complexity is Θ(n log n).

Page 22: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Makeheap in linear time

Tarjan’s algorithm: Building from the bottom to the top!

Page 23: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Makeheap in linear time

Tarjan’s algorithm: Building from the bottom to the top!

The bottom-most levels already form a set of heaps. In eachstep we add a new vertex on the next higher level thatcombines two of these heaps. The heap property can only beviolated in the root node.

Page 24: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Makeheap in linear time

Tarjan’s algorithm: Building from the bottom to the top!

The bottom-most levels already form a set of heaps. In eachstep we add a new vertex on the next higher level thatcombines two of these heaps. The heap property can only beviolated in the root node.

So for the added element we call the function bubble down.

Page 25: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Makeheap in linear time

Tarjan’s algorithm: Building from the bottom to the top!

The bottom-most levels already form a set of heaps. In eachstep we add a new vertex on the next higher level thatcombines two of these heaps. The heap property can only beviolated in the root node.

So for the added element we call the function bubble down.

Complexity:log n∑

i=1

(i−1)(

n2i

)

≤ n2·∞∑

i=0

i ·(

12i

)

= n2·∞∑

i=0

∞∑

j=i

12j

= n2

∞∑

i=0

12i−1 = 2n,

i.e., linear!

Page 26: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing extractmin

Observation: Minimum is in the root.

Page 27: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing extractmin

Observation: Minimum is in the root.

Remove the root and replace by...

Page 28: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing extractmin

Observation: Minimum is in the root.

Remove the root and replace by...

... the last element in the heap!

Page 29: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing extractmin

Observation: Minimum is in the root.

Remove the root and replace by...

... the last element in the heap!

Consolidate the heap, i.e., call bubble down

Page 30: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Implementing extractmin

Observation: Minimum is in the root.

Remove the root and replace by...

... the last element in the heap!

Consolidate the heap, i.e., call bubble down

Complexity: n-times call bubble down, i.e., a function withthe complexity bounded by the depth of the heap, i.e.,altogether O(n log n).

Page 31: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

How to represent a heap?

We could use dynamic data structures, but an array is muchmore efficient:

Page 32: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

How to represent a heap?

We could use dynamic data structures, but an array is muchmore efficient:

The root gets mapped onto position 1,

Page 33: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

How to represent a heap?

We could use dynamic data structures, but an array is muchmore efficient:

The root gets mapped onto position 1,

sons of the ith element appear on positions 2i and 2i + 1.

Page 34: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

How to represent a heap?

We could use dynamic data structures, but an array is muchmore efficient:

The root gets mapped onto position 1,

sons of the ith element appear on positions 2i and 2i + 1.

In this way we fill the array, root is at the beginning, the lastelement at the end.

Page 35: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

How to represent a heap?

We could use dynamic data structures, but an array is muchmore efficient:

The root gets mapped onto position 1,

sons of the ith element appear on positions 2i and 2i + 1.

In this way we fill the array, root is at the beginning, the lastelement at the end.

This representation is very efficient.

Page 36: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Mergesort

Another algorithm from the family of divide et imperatechniques.

Page 37: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Mergesort

Another algorithm from the family of divide et imperatechniques.

This time we do not care for the values, we always divide intohalves.

Page 38: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Mergesort

Another algorithm from the family of divide et imperatechniques.

This time we do not care for the values, we always divide intohalves.

Recursively we may obtain two sorted sequences and we haveto merge them.

Page 39: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Mergesort

Another algorithm from the family of divide et imperatechniques.

This time we do not care for the values, we always divide intohalves.

Recursively we may obtain two sorted sequences and we haveto merge them.

Merging can be performed in time linear w.r.t. length of theinput.

Page 40: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Mergesort

Another algorithm from the family of divide et imperatechniques.

This time we do not care for the values, we always divide intohalves.

Recursively we may obtain two sorted sequences and we haveto merge them.

Merging can be performed in time linear w.r.t. length of theinput.

When implementing Mergesort, we do not employ recursion,we rewrite it into several cycles.

Page 41: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity-analysis

We proceed by summing over the ”levels”, i.e., how long doesit take to merge sequences of length k into sequences oflength 2k?

Page 42: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity-analysis

We proceed by summing over the ”levels”, i.e., how long doesit take to merge sequences of length k into sequences oflength 2k?

Yes, linearly. How many lengths are needed (i.e., how manylevels take place)?

Page 43: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity-analysis

We proceed by summing over the ”levels”, i.e., how long doesit take to merge sequences of length k into sequences oflength 2k?

Yes, linearly. How many lengths are needed (i.e., how manylevels take place)?

Logarithmically (between two levels we double the length ofthe sorted sequence).

Page 44: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity-analysis

We proceed by summing over the ”levels”, i.e., how long doesit take to merge sequences of length k into sequences oflength 2k?

Yes, linearly. How many lengths are needed (i.e., how manylevels take place)?

Logarithmically (between two levels we double the length ofthe sorted sequence).

Thus altogether: O(n log n).

Page 45: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryout of core sorting

Internal memory is usually on-chip RAM.

Page 46: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryout of core sorting

Internal memory is usually on-chip RAM.

It is fast but relatively small.

Page 47: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryout of core sorting

Internal memory is usually on-chip RAM.

It is fast but relatively small.

External memory is a hard-disk or good old tape.

Page 48: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryout of core sorting

Internal memory is usually on-chip RAM.

It is fast but relatively small.

External memory is a hard-disk or good old tape.

It is large but slow. Here, it is an advantage when we read aslong block as possible (and we do not skip too much).

Page 49: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryout of core sorting

Internal memory is usually on-chip RAM.

It is fast but relatively small.

External memory is a hard-disk or good old tape.

It is large but slow. Here, it is an advantage when we read aslong block as possible (and we do not skip too much).

Usually we are sorting huge amounts of data that do notnecessarily fit into the internal memory.

Page 50: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryout of core sorting

Internal memory is usually on-chip RAM.

It is fast but relatively small.

External memory is a hard-disk or good old tape.

It is large but slow. Here, it is an advantage when we read aslong block as possible (and we do not skip too much).

Usually we are sorting huge amounts of data that do notnecessarily fit into the internal memory.

For sorting data in external memory, a mergesort can beperformed.

Page 51: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryimproving the algorithm

First we perform a heapsort in internal memory to obtain asorted block that can be written to external memory.

Page 52: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryimproving the algorithm

First we perform a heapsort in internal memory to obtain asorted block that can be written to external memory.

We modify this algorithm so that we do not just build theheap and extract by units, but if the next sorted element islarge enough, we add it to the current heap.

Page 53: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryimproving the algorithm

First we perform a heapsort in internal memory to obtain asorted block that can be written to external memory.

We modify this algorithm so that we do not just build theheap and extract by units, but if the next sorted element islarge enough, we add it to the current heap.

Large enough means at least as large as was the last elementwe wrote.

Page 54: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryimproving the algorithm

First we perform a heapsort in internal memory to obtain asorted block that can be written to external memory.

We modify this algorithm so that we do not just build theheap and extract by units, but if the next sorted element islarge enough, we add it to the current heap.

Large enough means at least as large as was the last elementwe wrote.

If the element is too small (smaller than the last element), wefinish this heap and start a new one.

Page 55: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting in external memoryimproving the algorithm

First we perform a heapsort in internal memory to obtain asorted block that can be written to external memory.

We modify this algorithm so that we do not just build theheap and extract by units, but if the next sorted element islarge enough, we add it to the current heap.

Large enough means at least as large as was the last elementwe wrote.

If the element is too small (smaller than the last element), wefinish this heap and start a new one.

Next we perform mergesort. Note that repeating heapsortbrings no improvement!

Page 56: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting on tapes

We have k tapes and we want to perform a mergesort onthem.

Page 57: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting on tapes

We have k tapes and we want to perform a mergesort onthem.

How to distribute the blocks onto tapes? It is an advantagewhen we merge from all (remaining) tapes onto one.

Page 58: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting on tapes

We have k tapes and we want to perform a mergesort onthem.

How to distribute the blocks onto tapes? It is an advantagewhen we merge from all (remaining) tapes onto one.

By generalized Fibonacci numbers. For 3 tapes:

Page 59: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting on tapes

We have k tapes and we want to perform a mergesort onthem.

How to distribute the blocks onto tapes? It is an advantagewhen we merge from all (remaining) tapes onto one.

By generalized Fibonacci numbers. For 3 tapes:

Last configuration (0, 0, 1), previous (1, 1, 0), (2, 0, 1), (0, 2, 3),i.e.,

Page 60: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting on tapes

We have k tapes and we want to perform a mergesort onthem.

How to distribute the blocks onto tapes? It is an advantagewhen we merge from all (remaining) tapes onto one.

By generalized Fibonacci numbers. For 3 tapes:

Last configuration (0, 0, 1), previous (1, 1, 0), (2, 0, 1), (0, 2, 3),i.e.,

(0, fi−2, fi−3), (fi−2, fi−1, 0), (fi , 0, fi−1), (0, fi , fi+1).

Page 61: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Sorting on tapes

We have k tapes and we want to perform a mergesort onthem.

How to distribute the blocks onto tapes? It is an advantagewhen we merge from all (remaining) tapes onto one.

By generalized Fibonacci numbers. For 3 tapes:

Last configuration (0, 0, 1), previous (1, 1, 0), (2, 0, 1), (0, 2, 3),i.e.,

(0, fi−2, fi−3), (fi−2, fi−1, 0), (fi , 0, fi−1), (0, fi , fi+1).

With more tapes, generalized Fibonacci numbers appear.

Page 62: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity of the problemsorting by comparison

Let us recall that the complexity of a problem is thecomplexity of the optimal algorithm solving this problem.

Page 63: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity of the problemsorting by comparison

Let us recall that the complexity of a problem is thecomplexity of the optimal algorithm solving this problem.

We have seen several algorithms for sorting, several of themwith complexity O(n log n). Can we do better or is there someobstruction?

Page 64: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity of the problemsorting by comparison

Let us recall that the complexity of a problem is thecomplexity of the optimal algorithm solving this problem.

We have seen several algorithms for sorting, several of themwith complexity O(n log n). Can we do better or is there someobstruction?

If we can gain information about the input only bycomparison...

Page 65: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity of the problemsorting by comparison

Let us recall that the complexity of a problem is thecomplexity of the optimal algorithm solving this problem.

We have seen several algorithms for sorting, several of themwith complexity O(n log n). Can we do better or is there someobstruction?

If we can gain information about the input only bycomparison...

... then we cannot do better.

Page 66: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Lower bound

For any deterministic sorting algorithm we can define adecision-tree.

Page 67: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Lower bound

For any deterministic sorting algorithm we can define adecision-tree.

The tree describes the comparisons (as the algorithm can onlydecide based on these comparisons). A leaf means that weknow how to permute the input.

Page 68: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Lower bound

For any deterministic sorting algorithm we can define adecision-tree.

The tree describes the comparisons (as the algorithm can onlydecide based on these comparisons). A leaf means that weknow how to permute the input.

We calculate the depth of this tree, i.e., depth of the deepestleaf.

Page 69: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Lower bound

For any deterministic sorting algorithm we can define adecision-tree.

The tree describes the comparisons (as the algorithm can onlydecide based on these comparisons). A leaf means that weknow how to permute the input.

We calculate the depth of this tree, i.e., depth of the deepestleaf.

How many leaves must there be?

Page 70: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Lower bound – cont.

At least one for each input permutation (differentpermutations have different inverse-permutations), i.e.,altogether at least n! leaves.

The decision-tree is a binary tree with n! leaves. What’s theminimum depth?

log n! ≥ log(nn2 ) = n

2log n, i.e., Ω(n log n).

Page 71: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Another proof

Different permutations require different computations.

Page 72: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Another proof

Different permutations require different computations.

How many computations are there with o(n log n)comparisons?

Page 73: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Another proof

Different permutations require different computations.

How many computations are there with o(n log n)comparisons?

o(2n log n), i.e., o(n!) – and it does not suffice to sort even allpossible permutations!

Page 74: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Another proof

Different permutations require different computations.

How many computations are there with o(n log n)comparisons?

o(2n log n), i.e., o(n!) – and it does not suffice to sort even allpossible permutations!

Among other, we showed the lower bound for Mergesort,Heapsort and even for A-sort as they belong to the family ofalgorithms sorting by comparison.

Page 75: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Bucketsortsorting in a linear time

Considering that the input consists of integers in a decimalnotation, we may split the values into ”buckets”based on avalue in a particular position.

Page 76: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Bucketsortsorting in a linear time

Considering that the input consists of integers in a decimalnotation, we may split the values into ”buckets”based on avalue in a particular position.

Bucketsort starts this splitting based on the last digit. Thenwe pick the content of individual buckets, put them one afteranother (in a correct ordering) and continue splitting based onthe previous digit up to the beginning of the (longest)numbers.

Page 77: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Bucketsortsorting in a linear time

Considering that the input consists of integers in a decimalnotation, we may split the values into ”buckets”based on avalue in a particular position.

Bucketsort starts this splitting based on the last digit. Thenwe pick the content of individual buckets, put them one afteranother (in a correct ordering) and continue splitting based onthe previous digit up to the beginning of the (longest)numbers.

Details on the blackboard.

Page 78: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Bucketsortcomplexity analysis

Considering the length of numbers is constant, what is thecomplexity?

Page 79: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Bucketsortcomplexity analysis

Considering the length of numbers is constant, what is thecomplexity?

c × n where c is a constant (depending on the length ofnumbers).

Page 80: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Bucketsortcomplexity analysis

Considering the length of numbers is constant, what is thecomplexity?

c × n where c is a constant (depending on the length ofnumbers).

We are sorting in a linear time. How is it possible (w.r.t. thelower bound)?

Page 81: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timemotivation

Quicksort was dividing the input based on a pivot.

Page 82: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timemotivation

Quicksort was dividing the input based on a pivot.

The complexity could be at most quadratic. Can we do better?

Page 83: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timemotivation

Quicksort was dividing the input based on a pivot.

The complexity could be at most quadratic. Can we do better?

Yes, if we manage to find a reasonable pivot in a linear time.

Page 84: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timemotivation

Quicksort was dividing the input based on a pivot.

The complexity could be at most quadratic. Can we do better?

Yes, if we manage to find a reasonable pivot in a linear time.

We design an algorithm looking for the kth largest value(among n values) in time linear w.r.t. n.

Page 85: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe algorithm itself

We divide the input into 5-tuples.

Page 86: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe algorithm itself

We divide the input into 5-tuples.

We find a median of each 5-tuple.

Page 87: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe algorithm itself

We divide the input into 5-tuples.

We find a median of each 5-tuple.

We find the median of these medians.

Page 88: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe algorithm itself

We divide the input into 5-tuples.

We find a median of each 5-tuple.

We find the median of these medians.

Divide the input onto pile of smaller values and larger values.

Page 89: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe algorithm itself

We divide the input into 5-tuples.

We find a median of each 5-tuple.

We find the median of these medians.

Divide the input onto pile of smaller values and larger values.

(Recursively) continue searching in the appropriate pile.

Page 90: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe details

How to find the medians of 5-tuples?

Page 91: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe details

How to find the medians of 5-tuples?

By brute force (because 5 is a constant).

Page 92: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe details

How to find the medians of 5-tuples?

By brute force (because 5 is a constant).

How to find the median of medians?

Page 93: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Median in a linear timethe details

How to find the medians of 5-tuples?

By brute force (because 5 is a constant).

How to find the median of medians?

Recursively (we are the function finding median in a lineartime) unless there are at most five 5-tuples (i.e., 5 candidatesfor median of medians).

Page 94: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

Page 95: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

The median of medians used as a pivot creates each new”pile”of size at most 7

10n (provided that the input is of size

n). Details on the blackboard.

Page 96: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

The median of medians used as a pivot creates each new”pile”of size at most 7

10n (provided that the input is of size

n). Details on the blackboard.

Complexity is: T (n) = cn + T(

n5

)

+ T(

710n)

Page 97: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

The median of medians used as a pivot creates each new”pile”of size at most 7

10n (provided that the input is of size

n). Details on the blackboard.

Complexity is: T (n) = cn + T(

n5

)

+ T(

710n)

We need to show: ∃d s. t. T (n) ≤ dn. This gets verified byinduction.

Page 98: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

The median of medians used as a pivot creates each new”pile”of size at most 7

10n (provided that the input is of size

n). Details on the blackboard.

Complexity is: T (n) = cn + T(

n5

)

+ T(

710n)

We need to show: ∃d s. t. T (n) ≤ dn. This gets verified byinduction.

Natural questions: Why 5-tuples? How about 3-tuples or7-tuples?

Page 99: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

The median of medians used as a pivot creates each new”pile”of size at most 7

10n (provided that the input is of size

n). Details on the blackboard.

Complexity is: T (n) = cn + T(

n5

)

+ T(

710n)

We need to show: ∃d s. t. T (n) ≤ dn. This gets verified byinduction.

Natural questions: Why 5-tuples? How about 3-tuples or7-tuples?

The latter works, the former not.

Page 100: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Complexity analysis

Why is the algorithm linear? Because the size of the”pile”reduces rapidly:

The median of medians used as a pivot creates each new”pile”of size at most 7

10n (provided that the input is of size

n). Details on the blackboard.

Complexity is: T (n) = cn + T(

n5

)

+ T(

710n)

We need to show: ∃d s. t. T (n) ≤ dn. This gets verified byinduction.

Natural questions: Why 5-tuples? How about 3-tuples or7-tuples?

The latter works, the former not.

Conclusion: Quicksort employing median in linear time hascomplexity Θ(n log n).

Page 101: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph – definition

Definition

A graph is an ordered pair G = (V ,E ) where V is a set of verticesand E ⊆

(

V2

)

is a set of edges.

Page 102: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph – definition

Definition

A graph is an ordered pair G = (V ,E ) where V is a set of verticesand E ⊆

(

V2

)

is a set of edges.

Definition

An ordered pair G = (V ,E ) is called an oriented graph with avertex set V and an edge set E , if E ⊆ V × V .

Page 103: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph – definition

Definition

A graph is an ordered pair G = (V ,E ) where V is a set of verticesand E ⊆

(

V2

)

is a set of edges.

Definition

An ordered pair G = (V ,E ) is called an oriented graph with avertex set V and an edge set E , if E ⊆ V × V .

Graphs are discussed in Discrete mathematics, so you alreadyknow some algorithms. Now we want to implement them.

Page 104: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph – definition

Definition

A graph is an ordered pair G = (V ,E ) where V is a set of verticesand E ⊆

(

V2

)

is a set of edges.

Definition

An ordered pair G = (V ,E ) is called an oriented graph with avertex set V and an edge set E , if E ⊆ V × V .

Graphs are discussed in Discrete mathematics, so you alreadyknow some algorithms. Now we want to implement them.

The definition of a graph is nice, but it does not help muchwith programming. So we have to ask:

Page 105: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph – definition

Definition

A graph is an ordered pair G = (V ,E ) where V is a set of verticesand E ⊆

(

V2

)

is a set of edges.

Definition

An ordered pair G = (V ,E ) is called an oriented graph with avertex set V and an edge set E , if E ⊆ V × V .

Graphs are discussed in Discrete mathematics, so you alreadyknow some algorithms. Now we want to implement them.

The definition of a graph is nice, but it does not help muchwith programming. So we have to ask:

How do we represent a graph while programming?

Page 106: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graphs – remarks to definition

Goal: Advantages and disadvantages of individualrepresentations.

Page 107: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graphs – remarks to definition

Goal: Advantages and disadvantages of individualrepresentations.

Define basic notions (walk, trail, path, connectivity, trees).

Page 108: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph Representation

From lectures of Discrete Mathematics you know:

Page 109: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph Representation

From lectures of Discrete Mathematics you know:

Adjacency matrix AG

– is a square 0/1-matrix whose rows and columns are indexedby individual vertices. One corresponds to an edge betweenappropriate vertices (zero means no edge there).

Page 110: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph Representation

From lectures of Discrete Mathematics you know:

Adjacency matrix AG

– is a square 0/1-matrix whose rows and columns are indexedby individual vertices. One corresponds to an edge betweenappropriate vertices (zero means no edge there).

Incidence matrix BG – rows index by vertices, columns byedges, one in B [i , j] means that the edge j is incident with thevertex i .

Page 111: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph Representation

From lectures of Discrete Mathematics you know:

Adjacency matrix AG

– is a square 0/1-matrix whose rows and columns are indexedby individual vertices. One corresponds to an edge betweenappropriate vertices (zero means no edge there).

Incidence matrix BG – rows index by vertices, columns byedges, one in B [i , j] means that the edge j is incident with thevertex i .

Advantages and disadvantages?

Page 112: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph Representation

From lectures of Discrete Mathematics you know:

Adjacency matrix AG

– is a square 0/1-matrix whose rows and columns are indexedby individual vertices. One corresponds to an edge betweenappropriate vertices (zero means no edge there).

Incidence matrix BG – rows index by vertices, columns byedges, one in B [i , j] means that the edge j is incident with thevertex i .

Advantages and disadvantages?

Can we convert these representations?

Page 113: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Converting AG to BG and back

init with 0s(BG);

edge index:=1;

for i:=1 to n do begin

for j:=i+1 to n do begin

if(AG [i , j]=1) then

begin

BG[i,edge index]:=1;

BG[j,edge index]:=1;

inc(edge index);

end;

end;

Page 114: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

BG to AG

Either we analyze the incidence matrix (in a similar way) or:AG := BG × BG

T ;for i:=1 to n do

AG [i , i ] := 0;

Dukaz.

Exercise in Combinatorics and Graph Theory I

Page 115: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

Page 116: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

Page 117: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

Page 118: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Page 119: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

Page 120: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

find neighbors(v),

Page 121: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

find neighbors(v),

vertices,

Page 122: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

find neighbors(v),

vertices,

edges or edge(u,v) – we can find it through vertices andfind neighbors,

Page 123: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

find neighbors(v),

vertices,

edges or edge(u,v) – we can find it through vertices andfind neighbors,

further, e.g., (vertex weight(v), edge weight(e)...).

Page 124: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

find neighbors(v),

vertices,

edges or edge(u,v) – we can find it through vertices andfind neighbors,

further, e.g., (vertex weight(v), edge weight(e)...).

Advantages/disadvantages?

Page 125: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph representations

List of vertices and edges incident to individual vertices.

I.e., we are keeping a list of edges incident to each vertex inthe graph.

We employ linked-lists. If we employ an array, what do we get?

The adjacency matrix!

Functions necessary/sufficient to work with a graph:

find neighbors(v),

vertices,

edges or edge(u,v) – we can find it through vertices andfind neighbors,

further, e.g., (vertex weight(v), edge weight(e)...).

Advantages/disadvantages?

In oriented case we have to modify the representation.

Page 126: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Walk, trail, path, circle

Definition

Page 127: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Walk, trail, path, circle

Definition

Walk of length k s a sequence of edgesv0, v1, v1, v2, v2, v3, . . . , vk−1, vk.

Page 128: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Walk, trail, path, circle

Definition

Walk of length k s a sequence of edgesv0, v1, v1, v2, v2, v3, . . . , vk−1, vk.

Trail is a walk where each edge occurs at most once.

Page 129: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Walk, trail, path, circle

Definition

Walk of length k s a sequence of edgesv0, v1, v1, v2, v2, v3, . . . , vk−1, vk.

Trail is a walk where each edge occurs at most once.

Path is a trail (or a walk) where each vertex occurs only once(i.e., each vertex is incident to two consecutive edges).

Page 130: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Walk, trail, path, circle

Definition

Walk of length k s a sequence of edgesv0, v1, v1, v2, v2, v3, . . . , vk−1, vk.

Trail is a walk where each edge occurs at most once.

Path is a trail (or a walk) where each vertex occurs only once(i.e., each vertex is incident to two consecutive edges).

A trail is a circle if it starts and ends in the same vertex andeach vertex occurs there exactly once.

Page 131: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Page 132: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Page 133: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Graph is a tree if it is connected and it contains no circles.

Page 134: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Graph is a tree if it is connected and it contains no circles.

Definitions are fine, but how we use them while programming?

Page 135: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Graph is a tree if it is connected and it contains no circles.

Definitions are fine, but how we use them while programming?

How do we verify that a graph is connected?

Page 136: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Graph is a tree if it is connected and it contains no circles.

Definitions are fine, but how we use them while programming?

How do we verify that a graph is connected?

We use a suitable claim.

Page 137: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Graph is a tree if it is connected and it contains no circles.

Definitions are fine, but how we use them while programming?

How do we verify that a graph is connected?

We use a suitable claim.

How do we decide whether a graph is a tree?

Page 138: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Connectivity, tree

Definition

Graph is connected if from any its vertex we can reach anyother vertex.

Graph is a tree if it is connected and it contains no circles.

Definitions are fine, but how we use them while programming?

How do we verify that a graph is connected?

We use a suitable claim.

How do we decide whether a graph is a tree?

Similarly!

Page 139: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Graph connectivityGraph is connected iff from one (fixed) vertex we can reach all the other vertices.

for i in vertices do

unvisit(i); so far we visited nothingi:=start vertex;

queue:=i;for reachable verticeswhile nonempty(queue) do begin

visit(i);

queue:=queue+unvisited neighbors(i);

end;

connected:=true;

for i in vertices do begin

if unvisited(i) then

connected:=false;

Page 140: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

Page 141: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Page 142: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Complexity depends on the representation (how quickly wefind the neighbors of a given vertex).

Page 143: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Complexity depends on the representation (how quickly wefind the neighbors of a given vertex).

Complexity is Ω(m) (Each edge must be inspected).

Page 144: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Complexity depends on the representation (how quickly wefind the neighbors of a given vertex).

Complexity is Ω(m) (Each edge must be inspected).

Considering a list of edges incident to each vertex, complexityis O(m),

Page 145: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Complexity depends on the representation (how quickly wefind the neighbors of a given vertex).

Complexity is Ω(m) (Each edge must be inspected).

Considering a list of edges incident to each vertex, complexityis O(m),

in the adjacency matrix, complexity is O(n2),

Page 146: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Complexity depends on the representation (how quickly wefind the neighbors of a given vertex).

Complexity is Ω(m) (Each edge must be inspected).

Considering a list of edges incident to each vertex, complexityis O(m),

in the adjacency matrix, complexity is O(n2),

considering the incidence matrix, complexity may be Θ(mn2).

Page 147: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Algorithm analysis

for-cycle takes place at most n-times.

while-cycle passes for each vertex at most once and inspectsthe neighbors of this vertex.

Complexity depends on the representation (how quickly wefind the neighbors of a given vertex).

Complexity is Ω(m) (Each edge must be inspected).

Considering a list of edges incident to each vertex, complexityis O(m),

in the adjacency matrix, complexity is O(n2),

considering the incidence matrix, complexity may be Θ(mn2).

Thus a good representation yields the complexity Θ(m + n).

Page 148: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If we use a queue, we are implementing the wave-algorithm(BFS).

Page 149: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If we use a queue, we are implementing the wave-algorithm(BFS).

We may use a buffer and get a DFS.

Page 150: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If we use a queue, we are implementing the wave-algorithm(BFS).

We may use a buffer and get a DFS.

Advantages/disadvantages:

Page 151: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If we use a queue, we are implementing the wave-algorithm(BFS).

We may use a buffer and get a DFS.

Advantages/disadvantages:

DFS may get implemented using recursion (and thus withoutan auxiliary data-structure).

Page 152: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If we use a queue, we are implementing the wave-algorithm(BFS).

We may use a buffer and get a DFS.

Advantages/disadvantages:

DFS may get implemented using recursion (and thus withoutan auxiliary data-structure).

BFS visits the vertex using the shortest path.

Page 153: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Looking for a cycleA graph has a cycle if we return to a particular vertex while searching the graph.

cycle:=false; so far no cyclefor i in vertices do unvisit(i);for i in vertices do

if unvisited(i) thennew componentbegin queue:=i;

while(nonempty(queue)) dobegin dequeue from queue and assing into(i);

if(visited(i)) thencycle:=true;

else for j in neighbors(i) dobegin queue:=queue+j;

erase edge(i,j);end;

end; end;

Page 154: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Tree

We may test whether the graph is connected without cycle(use previous algorithms).

Page 155: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Tree

We may test whether the graph is connected without cycle(use previous algorithms).

Or we test cycle-freeness and connectivity (one component).

Page 156: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Tree

We may test whether the graph is connected without cycle(use previous algorithms).

Or we test cycle-freeness and connectivity (one component).

Or we test connectivity (or cycle-freeness) and an appropriatenumber of edges (Euler’s formular).

Page 157: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Shortest path

When looking for the shortest path, it depends on therepresentation:

Perform BFS (considering the list of vertices and edges),

Theorem

In AGk position i , j gives number of walks with length k from

(vertex) i to j .

Corollary

In (AG + I )k position i , j says the number of walks of length atmost k from i to j.

Page 158: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Shortest path

When looking for the shortest path, it depends on therepresentation:

Perform BFS (considering the list of vertices and edges),

make the power of adjacency-matrix usingmatrix-representation.

Theorem

In AGk position i , j gives number of walks with length k from

(vertex) i to j .

Corollary

In (AG + I )k position i , j says the number of walks of length atmost k from i to j.

Page 159: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Dijkstra’s algorithmLooks for the shortest path from a given vertex into all other vertices

Input: Graph with nonnegatively evaluated edges.

We keep the ”queue”for vertices ordered by the shortest so farfound path.

At the beginning we inicialize the distances to all vertices[except start] by infinity [large-enough value], distance to startis 0.

We add start into the queue for reachable vertices.

Remove the first vertex of the ”queue”and inspect itsneighbors.

Repeat this while the ”queue”is non-empty.

Page 160: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Extending the path

When extending the path, for a vertex v in distance d(v) we tryfor each edge v ,w whether

d(w) > d(v) + length(v ,w).

If so, let d(w) := d(v) + length(v ,w) and correct the positionof w in the ”queue”.

Page 161: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Analysis

The algorithm is proven in Invitation to Discrete Mathematics(and many other books).

Page 162: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Analysis

The algorithm is proven in Invitation to Discrete Mathematics(and many other books).

Finiteness: Each iteration removes one vertex from the”queue”. This vertex never appears there anew (as all theedge-lengths are non-negative).

Page 163: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Analysis

The algorithm is proven in Invitation to Discrete Mathematics(and many other books).

Finiteness: Each iteration removes one vertex from the”queue”. This vertex never appears there anew (as all theedge-lengths are non-negative).

Partial correctness uses the invariant:In each iteration we have the shortes paths using only thevertices that were already removed from the ”queue”.

Page 164: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Analysis

The algorithm is proven in Invitation to Discrete Mathematics(and many other books).

Finiteness: Each iteration removes one vertex from the”queue”. This vertex never appears there anew (as all theedge-lengths are non-negative).

Partial correctness uses the invariant:In each iteration we have the shortes paths using only thevertices that were already removed from the ”queue”.

Invariant shows the correctness.

Page 165: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Analysis

The algorithm is proven in Invitation to Discrete Mathematics(and many other books).

Finiteness: Each iteration removes one vertex from the”queue”. This vertex never appears there anew (as all theedge-lengths are non-negative).

Partial correctness uses the invariant:In each iteration we have the shortes paths using only thevertices that were already removed from the ”queue”.

Invariant shows the correctness.

The algorithm is kind of modification of BFS!

Page 166: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Analysis

The algorithm is proven in Invitation to Discrete Mathematics(and many other books).

Finiteness: Each iteration removes one vertex from the”queue”. This vertex never appears there anew (as all theedge-lengths are non-negative).

Partial correctness uses the invariant:In each iteration we have the shortes paths using only thevertices that were already removed from the ”queue”.

Invariant shows the correctness.

The algorithm is kind of modification of BFS!

Complexity depends on the representation of the graph and ofthe ’queue’ !

Page 167: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If the graph is unweighted, Dijkstra’s algorithm collapses intoBFS!

Page 168: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If the graph is unweighted, Dijkstra’s algorithm collapses intoBFS!

Applications of graph algorithms – the whole theoreticalcomputer science.

Page 169: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If the graph is unweighted, Dijkstra’s algorithm collapses intoBFS!

Applications of graph algorithms – the whole theoreticalcomputer science.

Examples of graph-algorithms:

Page 170: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If the graph is unweighted, Dijkstra’s algorithm collapses intoBFS!

Applications of graph algorithms – the whole theoreticalcomputer science.

Examples of graph-algorithms:

Thesseus and Minotaurus,

Page 171: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If the graph is unweighted, Dijkstra’s algorithm collapses intoBFS!

Applications of graph algorithms – the whole theoreticalcomputer science.

Examples of graph-algorithms:

Thesseus and Minotaurus,

King (and other garbage) on chessboard with some squaresprohibited,

Page 172: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Remarks

If the graph is unweighted, Dijkstra’s algorithm collapses intoBFS!

Applications of graph algorithms – the whole theoreticalcomputer science.

Examples of graph-algorithms:

Thesseus and Minotaurus,

King (and other garbage) on chessboard with some squaresprohibited,

...

Page 173: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Page 174: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Eulerian graph, i.e., can we draw all the edges of the graph byone trail (without passing any edge twice)?

Page 175: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Eulerian graph, i.e., can we draw all the edges of the graph byone trail (without passing any edge twice)?

Hamiltonicity – a circle that visits all the vertices (eachexactly once),

Page 176: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Eulerian graph, i.e., can we draw all the edges of the graph byone trail (without passing any edge twice)?

Hamiltonicity – a circle that visits all the vertices (eachexactly once),

Clique – maximum complete subgraph,

Page 177: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Eulerian graph, i.e., can we draw all the edges of the graph byone trail (without passing any edge twice)?

Hamiltonicity – a circle that visits all the vertices (eachexactly once),

Clique – maximum complete subgraph,

Chromaticity – minimum number of colors s. t. we can colorall vertices in such a way that neighboring vertices havedistinct colors.

Page 178: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Eulerian graph, i.e., can we draw all the edges of the graph byone trail (without passing any edge twice)?

Hamiltonicity – a circle that visits all the vertices (eachexactly once),

Clique – maximum complete subgraph,

Chromaticity – minimum number of colors s. t. we can colorall vertices in such a way that neighboring vertices havedistinct colors.

Edge chromaticity – we color edges and no pair of edgesincident with a common vertex has the same color.

Page 179: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

Further graph-optimization problems (algorithms)

Minimum spanning tree – a.k.a. how to create the electricpower lines.

Eulerian graph, i.e., can we draw all the edges of the graph byone trail (without passing any edge twice)?

Hamiltonicity – a circle that visits all the vertices (eachexactly once),

Clique – maximum complete subgraph,

Chromaticity – minimum number of colors s. t. we can colorall vertices in such a way that neighboring vertices havedistinct colors.

Edge chromaticity – we color edges and no pair of edgesincident with a common vertex has the same color.

...

Page 180: Overview - kam.mff.cuni.czperm/programovani/old/... · Heap A heap is a tree where for each vertex (node) it holds that the value of the parent is at most as large as the value of

Long numbers Sorting Graphs Graph Representation Definition Graph properties Algorithms

End

Thank you for your attention...