59
Paths in a Graph : A Brief Tutorial Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University 1

Paths in a Graph : A Brief Tutorial

  • Upload
    nam

  • View
    49

  • Download
    2

Embed Size (px)

DESCRIPTION

Paths in a Graph : A Brief Tutorial. Krishna.V.Palem Kenneth and Audrey Kennedy Professor of Computing Department of Computer Science, Rice University. Introduction. Many problems can be modeled using graphs with weights assigned to their edges: Airline flight times - PowerPoint PPT Presentation

Citation preview

Page 1: Paths in a Graph : A Brief Tutorial

Paths in a Graph : A Brief Tutorial

Krishna.V.PalemKenneth and Audrey Kennedy Professor of ComputingDepartment of Computer Science, Rice University

1

Page 2: Paths in a Graph : A Brief Tutorial

IntroductionMany problems can be modeled using graphs

with weights assigned to their edges:Airline flight timesTelephone communication costsComputer networks response times

Tokyo Subway

Map

2

Page 3: Paths in a Graph : A Brief Tutorial

Weighted Graphs In a weighted graph, each edge has an associated

numerical value, called the weight of the edge Edge weights may represent, distances, costs, etc. Example:

In a flight route graph, the weight of an edge represents the distance in miles between the endpoint airports

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

1205

3

Page 4: Paths in a Graph : A Brief Tutorial

Shortest Path Problem Given a weighted graph and two vertices u and v, we want

to find a path of minimum total weight between u and v.Length of a path is the sum of the weights of its edges.

Example:Shortest path between Providence and Honolulu

Applications Internet packet routing Flight reservationsDriving directions

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

1205

4

Page 5: Paths in a Graph : A Brief Tutorial

Dijkstra’s Algorithm to compute the Shortest Path G is a simple connected graph.

A simple graph G = (V, E) consists of V, a nonempty set of vertices, and E, a set of unordered pairs of distinct elements of V called edges.

Each edge has an associated weight It is a greedy algorithm

A greedy algorithm is any algorithm that follows the problem solving metaheuristic of making the locally optimal choice at each stage with the hope of finding the global optimum.

5

Page 6: Paths in a Graph : A Brief Tutorial

Dijkstra’s Algorithm

The distance of a vertex v from a vertex s is the length of a shortest path between s and v

Dijkstra’s algorithm computes the distances of all the vertices from a given start vertex s

Assumptions: the graph is connected the edges are

undirected the edge weights are

nonnegative

We grow a “cloud” of vertices, beginning with s and eventually covering all the vertices

We store with each vertex v a label d(v) representing the distance of v from s in the subgraph consisting of the cloud and its adjacent vertices

At each step We add to the cloud the vertex

u outside the cloud with the smallest distance label, d(u)

We update the labels of the vertices adjacent to u 6

Page 7: Paths in a Graph : A Brief Tutorial

Application of Dijkstra’s AlgorithmFind shortest path from s to t using

Dijkstra’s algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

7

Page 8: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

0

distance label

S = { }

P = { s, 2, 3, 4, 5, 6, 7, t }

8

Page 9: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

0

distance label

S = { }

P = { s, 2, 3, 4, 5, 6, 7, t }delmin

9

Page 10: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

distance label

S = { s }

P = { 2, 3, 4, 5, 6, 7, t }decrease key

X

X

X10

Page 11: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

distance label

S = { s }

P = { 2, 3, 4, 5, 6, 7, t }

X

X

X

delmin

11

Page 12: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2 }

P = { 3, 4, 5, 6, 7, t }

X

X

X12

Page 13: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2 }

P = { 3, 4, 5, 6, 7, t }

X

X

X

decrease key

X 33

13

Page 14: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2 }

P = { 3, 4, 5, 6, 7, t }

X

X

X

X 33

delmin

14

Page 15: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6 }

P = { 3, 4, 5, 7, t }

X

X

X

X 33

44X

X

32

15

Page 16: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6 }

P = { 3, 4, 5, 7, t }

X

X

X

44X

delmin

X 33X

32

16

Page 17: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6, 7 }

P = { 3, 4, 5, t }

X

X

X

44X

35X

59 X

24

X 33X

32

17

Page 18: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 6, 7 }

P = { 3, 4, 5, t }

X

X

X

44X

35X

59 X

delmin

X 33X

32

18

Page 19: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 6, 7 }

P = { 4, 5, t }

X

X

X

44X

35X

59 XX 51

X 34

X 33X

32

19

Page 20: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 6, 7 }

P = { 4, 5, t }

X

X

X

44X

35X

59 XX 51

X 34

delmin

X 33X

32

24

20

Page 21: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 5, 6, 7 }

P = { 4, t }

X

X

X

44X

35X

59 XX 51

X 34

24

X 50

X 45

X 33X

32

21

Page 22: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 5, 6, 7 }

P = { 4, t }

X

X

X

44X

35X

59 XX 51

X 34

24

X 50

X 45

delmin

X 33X

32

22

Page 23: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7 }

P = { t }

X

X

X

44X

35X

59 XX 51

X 34

24

X 50

X 45

X 33X

32

23

Page 24: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7 }

P = { t }

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

delmin

X 33X

32

24

24

Page 25: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7, t }

P = { }

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

X 33X

32

25

Page 26: Paths in a Graph : A Brief Tutorial

Dijkstra's Shortest Path Algorithm

s

3

t

2

6

7

4

5

24

18

2

9

14

15 5

30

20

44

16

11

6

19

6

15

9

14

0

S = { s, 2, 3, 4, 5, 6, 7, t }

P = { }

X

X

X

44X

35X

59 XX 51

X 34

X 50

X 45

X 33X

32

26

Page 27: Paths in a Graph : A Brief Tutorial

In-Class Exercise Find the shortest route to reach Honolulu (HNL) from

Providence (PVD)Use Dikjstra’s algorithm

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

1205

27

Page 28: Paths in a Graph : A Brief Tutorial

Why It Doesn’t Work for Negative-Weight Edges

If a node with a negative incident edge were to be added late to the cloud, it could mess up distances for vertices already in the cloud.

CB

A

E

D

F

0

457

5 9

48

7 1

2 5

6

0 -8

Dijkstra’s algorithm is based on the greedy method. It adds vertices by increasing distance.

C’s true distance is 1, but it is already in the cloud with d(C)=5!

28

Page 29: Paths in a Graph : A Brief Tutorial

Remarks on Dijkstra’s Shortest Path Algorithm

Dijkstra’s algorithm doesn’t account for graphs whose edges may have negative weightsBellman-Ford’s algorithm accounts for negative

weight pathsDijkstra’s algorithm works for a single source

and a single sink pair Floyd’s algorithm solves for the shortest path among

all pairs of vertices.Dijkstra’s shortest path algorithm can be

solved in polynomial time in graphs without negative-weight cyclesIt takes O(E+VlogV) time

29

Page 30: Paths in a Graph : A Brief Tutorial

Bellman-Ford algorithm

Bellman-Ford Algorithm takes O(E*V) time30

Page 31: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT

We fix the source node as A

31

Page 32: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT32

Page 33: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT33

Page 34: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT34

Page 35: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT35

Page 36: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT36

Page 37: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT37

Page 38: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT38

Page 39: Paths in a Graph : A Brief Tutorial

Example of Bellman-Ford Algorithm

Courtesy: Eric Demaine, MIT39

Page 40: Paths in a Graph : A Brief Tutorial

TractabilitySome problems are undecidable: no computer

can solve themE.g., Turing’s “Halting Problem”

Other problems are decidable, but intractable: as they grow large, we are unable to solve them

in reasonable timeWhat constitutes “reasonable time”?

Some problems are provably decidable in polynomial time on an ordinary computerWe say such problems belong to the set PTechnically, a computer with unlimited memory

40

Page 41: Paths in a Graph : A Brief Tutorial

NPSome problems are provably decidable in

polynomial time on a nondeterministic computerWe say such problems belong to the set NPCan think of a nondeterministic computer as a

parallel machine that can freely spawn an infinite number of processes

P = set of problems that can be solved in polynomial time

NP = set of problems for which a solution can be verified in polynomial time

P NPThe big question: Does P = NP?

41

Page 42: Paths in a Graph : A Brief Tutorial

NP-CompletenessHow would you define NP-Complete?They are the “hardest” problems in NP

PNP

NP-Complete

42

Page 43: Paths in a Graph : A Brief Tutorial

NP-CompletenessThe NP-Complete problems are an interesting class of

problems whose status is unknown No polynomial-time algorithm has been discovered for an NP-

Complete problemNo superpolynomial lower bound has been proved for any NP-

Complete problem, eitherThe two chief characteristics of NP-complete problems are :

NP-complete is a subset of NP set of all decision problems whose solutions can be verified in polynomial

time; A problem s in NP is also in NP-complete if and only if every other

problem in NP can be transformed into s in polynomial timeUnless there is a dramatic change in the current thinking,

there is no “efficient time” algorithm which can solve NP-complete problems A brute-force search is often required

43

Page 44: Paths in a Graph : A Brief Tutorial

Longest Path AlgorithmThe longest path problem is the problem of

finding a simple path of maximum length in a given graph.

Unlike the shortest path problem, the longest path problem is NP-completeThe optimal solution cannot be found in polynomial

time unless P = NP.The NP-completeness of the decision problem

can be shown using a reduction from the Hamiltonian path problem. If a certain general graph has a Hamiltonian path,

this Hamiltonian path is the longest path possible, as it traverses all possible vertices.

44

Page 45: Paths in a Graph : A Brief Tutorial

Longest Path AlgorithmA Hamiltonian path (or traceable path) is a

path in an undirected graph which visits each vertex exactly once.

A Hamiltonian cycle (or Hamiltonian circuit) is a cycle in an undirected graph which visits each vertex exactly once and also returns to the starting vertex.

Determining whether such paths and cycles exist in graphs is the Hamiltonian path problem which is NP-complete.

45

Page 46: Paths in a Graph : A Brief Tutorial

Statistics

Krishna.V.PalemKenneth and Audrey Kennedy Professor of ComputingDepartment of Computer Science, Rice University

46

Page 47: Paths in a Graph : A Brief Tutorial

ContentsHistory of StatisticsBasic terms involved in StatisticsSamplingExamples & In-Class ExerciseEstimation theory

47

Page 48: Paths in a Graph : A Brief Tutorial

History of Statistics

48

Page 49: Paths in a Graph : A Brief Tutorial

ContentsHistory of StatisticsBasic terms involved in StatisticsSamplingExamples & In-Class ExerciseEstimation theory

49

Page 50: Paths in a Graph : A Brief Tutorial

Basic Terms Involved in Statistics

To understand sampling, you need to first understand a few basic definitions. The total set of observations that can be made

is called the population. A sample is a collection of observations made

It is usually a much smaller subset of the populationA parameter is a measurable characteristic of

a population, such as a mean or standard deviation.

A statistic is a measurable characteristic of a sample, such as a mean or standard deviation.

50

Page 51: Paths in a Graph : A Brief Tutorial

Terminology UsedThe notation used to describe these

measures appears below: N: Number of observations in the population. n: Number of observations in the sample. μ: The population mean. x: The sample mean. σ2: The variance of the population. σ: The standard deviation of the population. s2: The variance of the sample. s: The standard deviation of the sample.

51

Page 52: Paths in a Graph : A Brief Tutorial

ContentsHistory of StatisticsBasic terms involved in StatisticsSamplingExamples & In-Class ExerciseEstimation theory

52

Page 53: Paths in a Graph : A Brief Tutorial

Sampling TheorySimple random sampling refers to a sampling

method that has the following properties. population consists of N objects. sample consists of n objects. all possible samples of n objects are equally likely

to occur.

The main benefit of simple random sampling is that it guarantees that the sample chosen is representative of the population.ensures that the statistical conclusions will be valid.

53

Page 54: Paths in a Graph : A Brief Tutorial

Mean & Variance of the Sample

The sample mean is the arithmetic average of the values in a random sampleIt is denoted by:

x = (x1+x2… +xn)/n = 1/n * Σ xi Since it is taken from a random sample, x is a

random variableThe variance of a sample is the average squared

deviation from the sample meanIt is denoted by:

s2 = Σ ( xi - x )2 / ( n - 1 )

where s2 is the sample variance, x is the sample mean, xi is the ith element from the sample, and n is the number of elements in the sample

Note: Each xi can be defined as a random variable 54

Page 55: Paths in a Graph : A Brief Tutorial

Computing the Mean of PopulationWe have a sample with mean x and

variance s2

We know that μ is population mean & x is sample mean.

How do we compute the mean of the population using this?

)x(E

xExEn*n

1)x(E

xEn

1x

n

1E)x(E

11

n

1ii

n

1ii Since, E(cX) = c E(X)

Since, E(x1) = E(x2) … = μ

Hence, population mean can be estimated by computing the expectation of the sample mean.

55

Page 56: Paths in a Graph : A Brief Tutorial

Computing the Variance of PopulationWe have a sample with mean x and variance s2

We know that μ is population mean & x is sample mean.

We know that Var(x) = σ2/n (from slide 6 of lecture 11)

In the next slide, we have a skeleton for the proof.Use it to derive the proof yourself as an in-class exercise

How do we compute variance of the population using this?

56

Page 57: Paths in a Graph : A Brief Tutorial

22

221

2

1

222

1

2

2

)(

)()(1

1)(

1

1)(

1)(

sE

xnExnEn

sE

xnxEn

sE

n

xxEsE

n

ii

n

ii

(From definition of s2)

(Use E(A+B) = E(A)+E(B) )

(Use x is constant & x =1/n Σ xi )

(Use, E(x1) = E(x2) … = E(xn) )

(Substitute, Var(X) =σ2 = E(X2)- μ2 &

Var(x) =σ2/n = E(X2)- μ2 )

Expand the Squared expression on the RHS

In-class exercise : Derivation of variance of Population

Hints

57

Page 58: Paths in a Graph : A Brief Tutorial

22

22

222

221

2

n

1i

22i

2

n

1i

22i

2

n

1i

222i

2

n

1i

n

1ii

n

1i

22i

2

n

1ii

22i

2

n

1i

2

i2

)s(E

)n(n)(n

1n

1)s(E

)x(nE)x(nE1n

1)s(E

)x(nE)x(E1n

1)s(E

xnxE1n

1)s(E

xn2xnxE1n

1)s(E

xx2xxE1n

1)s(E

xx2xxE1n

1)s(E

1n

xxE)s(E

(From definition of s2)

(Since, E(A+B) = E(A)+E(B) )

(Since, x is constant & x =1/n Σ xi )

(Since, E(x1) = E(x2) … = E(xn) )

(Since, Var(X) =σ2 = E(X2)- μ2 &

Var(x) =σ2/n = E(X2)- μ2 )

Complete Solution

58

Page 59: Paths in a Graph : A Brief Tutorial

Computing the Variance of PopulationWe have a sample with mean x and

variance s2

We know that μ is population mean & x is sample mean.

We know that Var(x) = σ2/n

How do we compute variance of the population using this?

Hence, population variance can be estimated by computing the expectation of the sample variance.

From the derivation, we obtained

E(s2) = σ2

59