applications data structures and algorithms (60-254)

71
Applications Data Structures and Algorithms (60- 254)

Upload: louisa-wilcox

Post on 15-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Applications Data Structures and Algorithms (60-254)

ApplicationsData Structures and Algorithms (60-254)

Page 2: Applications Data Structures and Algorithms (60-254)

2

Encoding and Decoding Messages

Page 3: Applications Data Structures and Algorithms (60-254)

3

Fixed-length coding

Code for each character has the same number of bits.

QuestionHow many bits do we need to encode uniquely each character in a message made up of characters from an n-letter alphabet? Answer log n bits at least.

Page 4: Applications Data Structures and Algorithms (60-254)

4

Variable-length coding

Page 5: Applications Data Structures and Algorithms (60-254)

5

Another encoding scheme

Page 6: Applications Data Structures and Algorithms (60-254)

6

Prefix codes

Code word of a character is NOT prefix of the code for a character , where .Also, called prefix-free because the term “prefix” is misleading… Meaning of prefixExamples:

00 is prefix of 001110 is prefix of 110111111101 is not prefix of 1111001

Page 7: Applications Data Structures and Algorithms (60-254)

7

What is interesting in a prefix code?

Page 8: Applications Data Structures and Algorithms (60-254)

8

Lossless Data Compression

M = a d d d

Encoding using scheme 1

C1= 000111Encoding using scheme 2

C2=00111111

Length(C1)=6

Length(C2)=8 Lossless data compression is possible using prefix codes

Page 9: Applications Data Structures and Algorithms (60-254)

9

Huffman coding

Page 10: Applications Data Structures and Algorithms (60-254)

10

Huffman coding

Page 11: Applications Data Structures and Algorithms (60-254)

11

Huffman coding

Page 12: Applications Data Structures and Algorithms (60-254)

12

Huffman coding

Page 13: Applications Data Structures and Algorithms (60-254)

13

Huffman coding

Page 14: Applications Data Structures and Algorithms (60-254)

14

Huffman coding

Page 15: Applications Data Structures and Algorithms (60-254)

15

Huffman coding

Page 16: Applications Data Structures and Algorithms (60-254)

16

Huffman coding

Page 17: Applications Data Structures and Algorithms (60-254)

17

Huffman coding

Page 18: Applications Data Structures and Algorithms (60-254)

18

Huffman coding

Page 19: Applications Data Structures and Algorithms (60-254)

19

Graphs

Page 20: Applications Data Structures and Algorithms (60-254)

20

Graphs

We denote a graph by G=(V, E) where V = Set of vertices andE = Set of edges

|V| = Number of vertices and|E| = Number of edges

G is directed if the edges are, otherwise G is undirected.

Page 21: Applications Data Structures and Algorithms (60-254)

21

Graph representation

Page 22: Applications Data Structures and Algorithms (60-254)

22

Graph representation

Page 23: Applications Data Structures and Algorithms (60-254)

23

Graph Representation

Page 24: Applications Data Structures and Algorithms (60-254)

24

Breadth-first search

Also called: level order search

What does it do? Given: G=(V, E), a directed or undirected graph and a “source” vertex s V It systematically explores vertices reachable from s, to construct a breadth-first search tree

Page 25: Applications Data Structures and Algorithms (60-254)

25

Shortest Path

Length of the path from s to each reachable vertex is the shortest path from s to this vertex Example graph

Page 26: Applications Data Structures and Algorithms (60-254)

26

Expand node s

Page 27: Applications Data Structures and Algorithms (60-254)

27

Expand node w

Page 28: Applications Data Structures and Algorithms (60-254)

28

Expand node r

Page 29: Applications Data Structures and Algorithms (60-254)

29

Expand node t

Page 30: Applications Data Structures and Algorithms (60-254)

30

Expand node x

Page 31: Applications Data Structures and Algorithms (60-254)

31

Expand node v

Page 32: Applications Data Structures and Algorithms (60-254)

32

Expand node u

Page 33: Applications Data Structures and Algorithms (60-254)

33

Breath-first Search Tree

Page 34: Applications Data Structures and Algorithms (60-254)

34

Formal Algorithm

Colour terminology:Gray nodes make up the “frontier” (red line) White nodes (vertices) are the unexplored ones Black nodes are the completely explored ones, lying on the other side of the “frontier”

Page 35: Applications Data Structures and Algorithms (60-254)

35

Breadth-first search algorithm

BFS(G, s) For each vertex uV –{s} { // Setup loop O(|V|)

u.color WHITE;u.distance ;u.parent NULL; // parent of u

}s.color GRAY;s.distance 0;s.parent NULL;Q Q {s};

(Cont’d.)

Page 36: Applications Data Structures and Algorithms (60-254)

36

Breadth-first search algorithm

While (Q ) { // Processing loop O(|E|)u head (Q);for each v adjacent to u {

if (v.color = WHITE) {v.color GRAY;v.distance u.distance+1;v.parent u;Q Q {v};

}}Delete u from Q;u.color BLACK;

}

Page 37: Applications Data Structures and Algorithms (60-254)

37

Analysis

Thm. Running time of BFS is O (|V|+|E|) Pf Queuing operations take O (|V|) time.Each edge in the graph is looked at most once. Hence O (|E|) time and the theorem follows.

Page 38: Applications Data Structures and Algorithms (60-254)

38

We can prove the following factsFact1:Breath-first-search (BFS) discovers every

vertex reachable from s Fact2:The length of the path from s to a reachable

vertex v, is a shortest path from s to v.

Page 39: Applications Data Structures and Algorithms (60-254)

39

Weighted Graph

Each edge has a weight (a number).

Page 40: Applications Data Structures and Algorithms (60-254)

40

Minimum Spanning Tree (MST)

Green edges form a minimum spanning tree of the example graph

Page 41: Applications Data Structures and Algorithms (60-254)

41

Minimum Spanning Tree

Weight of the MST is: W(T) = 8 + 2 + 4 + 7 + 4 + 2 + 9 + 1 = 37

Page 42: Applications Data Structures and Algorithms (60-254)

42

Formally

Given a connected, weighed, undirected graph G=(V, E), Find an acyclic subset TE that connects all the vertices such that

is minimum, where weight(u,v) is the weight of the edges connecting u to v.

Page 43: Applications Data Structures and Algorithms (60-254)

43

Growing an MST, greedily

Let A be a subset of edges of some MST

Def. An edge e=(u,v) of G is safe for A if A {(u,v)} is also a subset of an MST.

Page 44: Applications Data Structures and Algorithms (60-254)

44

A generic MST algorithm

GENERIC_MST(G,w) A ;

while (A does not form an MST)

find a safe edge (u,v) for A; A A {(u,v)};

return A;

Page 45: Applications Data Structures and Algorithms (60-254)

45

Cuts and safe edges

Definition: A cut of G is a partition (S, V-S) of V

Page 46: Applications Data Structures and Algorithms (60-254)

46

A few definitions

Definition: An edge (u,v) E crosses the cut (S,V-S) if one of its end points is in S and the other in V-S Definition: A cut respects the set A if no edge in A crosses the cut Definition: An edge is a light edge crossing a cut if its weight is the minimum of any edge crossing the cut.

Page 47: Applications Data Structures and Algorithms (60-254)

47

Safe Edges

The following theorem characterizes a safe edge for A TheoremLet A be a subset of E that is included in some MST for G. Let (S,V-S) be any cut of G that respects A, and Let (u,v) be a light edge crossing (S,V-S). Then, edge (u,v) is safe for A.

Page 48: Applications Data Structures and Algorithms (60-254)

48

Prim’s algorithm

Select vertex aS={a}

Page 49: Applications Data Structures and Algorithms (60-254)

49

(a, b) is a light edge for (S, V-S)

Select vertex bnew S={a, b}

Page 50: Applications Data Structures and Algorithms (60-254)

50

(b,c) is a light edge for (S,V-S)

Select vertex cnew S={a, b, c}

Page 51: Applications Data Structures and Algorithms (60-254)

51

(c, i) is a light edge for (S,V-S) where S={a, b, c}Select vertex inew S={a, b, c, i}

Page 52: Applications Data Structures and Algorithms (60-254)

52

(c, f) is a light edge for (S,V-S)

Select vertex fnew S={a, b, c, f, i}

Page 53: Applications Data Structures and Algorithms (60-254)

53

(f, g) is a light edge for (S,V-S)

Select vertex gnew S={a, b, c, g, f, i}

Page 54: Applications Data Structures and Algorithms (60-254)

54

(g, h) is a light edge for (S,V-S)

Select vertex hnew V-S={d, e}

Page 55: Applications Data Structures and Algorithms (60-254)

55

(c, d) is a light edge for (S,V-S)

Select vertex dnew V-S={e}

Page 56: Applications Data Structures and Algorithms (60-254)

56

(d, e) is a light edge for (S,V-S)

Select vertex enew V-S=

Page 57: Applications Data Structures and Algorithms (60-254)

57

Formal Algorithm

MST_Prim(G, W, r) QV;for (each u Q)

u.key ;

r.key 0;r.parent NULL;while ( Q )

u extract_min(Q);for (each v adjacent to u) if v Q and weight(u,v) < v.keyv.parent u;v.key weight(u,v)

Page 58: Applications Data Structures and Algorithms (60-254)

58

Important Note on Shortest Path: Dijkstra’s algorithmThe algorithm incrementally constructs a set S of vertices to which the shortest path is known.

It adds a vertex v (from the remaining vertices) to S,whose distance from S (source) is

the shortest of the remaining vertices. The assumption of non-negative weights guarantees that the shortest path to v passes only through vertices in S.

Page 59: Applications Data Structures and Algorithms (60-254)

59

Priority Queues

Let S be a set of keys, a priority queue supports the following operations on S: 1. Insert (S, x)2. Minimum (S) -- returns min key3. Extract_Min (S) -- returns and removes min key

Page 60: Applications Data Structures and Algorithms (60-254)

60

Heap as Priority Queue

A priority queue can be implemented using a heap A heap is a complete binary tree, satisfying the heap property:

For a node v,v.key v.left.keyv.key v.right.key

Page 61: Applications Data Structures and Algorithms (60-254)

61

Single-source shortest paths

Let G be a weighted, directed graph.

A path in the graph from u to v is a sequence of edges that connects u to v. The weight of this path is the sum of the weights of its constituent edges. Let (u, v) denote a minimum weight path from u to v Let s be a source vertex of G

Page 62: Applications Data Structures and Algorithms (60-254)

62

Single-source shortest path problemCompute a shortest path from s to each vertex v. Remark: A shortest path is a path of minimum weight. Dijkstra’s algorithm:

We assume that the weights of the edges are non-negative.

Page 63: Applications Data Structures and Algorithms (60-254)

63

Relaxation

Page 64: Applications Data Structures and Algorithms (60-254)

64

Relax

Relax(u,v,w) {If (v.distance > u.distance +w(u,v)) {

v.distance u.distance +w(u,v);v.parent u;

}}

Initialize_Simple_Source (G, v) {For each vertex v V [G];v.distance ;v.parent NULL;}

Page 65: Applications Data Structures and Algorithms (60-254)

65

Dijkstra’s algorithm by example

The algorithm maintains a set X of vertices whose final shortest-path weights from s have already been determined

Page 66: Applications Data Structures and Algorithms (60-254)

66

Add s to X

Relax vertices adjacent to s

Page 67: Applications Data Structures and Algorithms (60-254)

67

Add x to X

Relax vertices adjacent to x

Page 68: Applications Data Structures and Algorithms (60-254)

68

Add y to X

Relax vertices adjacent to y

Page 69: Applications Data Structures and Algorithms (60-254)

69

Add u to X

Relax vertices adjacent to u

Page 70: Applications Data Structures and Algorithms (60-254)

70

Add v to X

Done

Page 71: Applications Data Structures and Algorithms (60-254)

71

Formal Algorithm

Dijkstra(G,w,s) Initialize_single_source(G,s)

XQV [G]while (Q )

uextract_min(Q)XX{u}For each vertex v adjacent to u

Relax(u,v,w)