paths in a graph : a brief tutorial
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 PresentationTRANSCRIPT
Paths in a Graph : A Brief Tutorial
Krishna.V.PalemKenneth and Audrey Kennedy Professor of ComputingDepartment of Computer Science, Rice University
1
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Bellman-Ford algorithm
Bellman-Ford Algorithm takes O(E*V) time30
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT
We fix the source node as A
31
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT32
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT33
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT34
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT35
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT36
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT37
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT38
Example of Bellman-Ford Algorithm
Courtesy: Eric Demaine, MIT39
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
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
NP-CompletenessHow would you define NP-Complete?They are the “hardest” problems in NP
PNP
NP-Complete
42
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
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
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
Statistics
Krishna.V.PalemKenneth and Audrey Kennedy Professor of ComputingDepartment of Computer Science, Rice University
46
ContentsHistory of StatisticsBasic terms involved in StatisticsSamplingExamples & In-Class ExerciseEstimation theory
47
History of Statistics
48
ContentsHistory of StatisticsBasic terms involved in StatisticsSamplingExamples & In-Class ExerciseEstimation theory
49
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
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
ContentsHistory of StatisticsBasic terms involved in StatisticsSamplingExamples & In-Class ExerciseEstimation theory
52
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
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
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
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
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
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
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