answering distance queries in directed graphs using fast matrix multiplication seminar in algorithms...

42
Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Post on 21-Dec-2015

224 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Answering Distance Queries in directed

graphs using fast matrix multiplication

Seminar in AlgorithmsProf. Haim Kaplan

Lecture by Lior Eldar1/07/2007

Page 2: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Structure of Lecture

• Introduction & History

• Alg1 – APSP

• Alg2 – preprocess & query

• Alg3 – Hybrid

• Summary

Page 3: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Problem Definition• Given a weighted directed graph,

we are requested to find:– APSP - All pairs shortest paths – find for any pair– SSSP - Single Source shortest paths – find all

distances from s.

• A hybrid problem comes to mind:– Preprocess the graph faster than APSP– Answer ANY two-node distance query faster than

SSSP.– What’s it good for?

),,( wEVG

Page 4: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Previously known results – APSP

• Undirected graphs– Approximated algorithm by Thorup and Zwick:

• Preprocess undirected weighted graph in

expected time.

• Generate data structure of size

• Answer any query in O(1)

• BUT: answer is approximate with a factor of 2k-1.

– For non-negative integer weights at most M – Shoshan and Zwick developed an algorithm of run time

kmnO /1

knO /11

376.2, MnO

• Directed graphs – Zwick - runs in 58.268.0 nMO

Page 5: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Previously known results - SSSP

• Positive weights:– Directed graphs with positive weights – Dijkstra

with – Undirected graphs with positive integer edge

weights – Thorup with

• Negative weights – much harder:– Bellman-Ford– Goldberg and Tarjan – assumes edge weight

values are at least – N.

nnmO log

mO

mnO

NnmO log

Page 6: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

New Algorithm by Yuster / Zwick

• Solves the hybrid pre-processing-query problem for:– Directed graphs

– Integer weights from –M to M

• Achieves the following performance:– Pre-processing

– Query answering – O(n)

• Faster than previously known APSP (Zwick) so long as the number of queries is

• Better than SSSP performance (Goldberg&Tarjan) for dense graphs with small alphabet – gap of

376.2, MnO

158.2 MnO

13.0nO

Page 7: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Beyond the numbers…• An extension of this algorithm allows complete

freedom in optimization of the pre-processing - query problem.

• to optimize an algorithm for an arbitrary number of queries q, we want: preprocessing time + q * query time to be minimal.

• This defines the ratio between query time and pre-processing time - completely controlled by the algorithm inputs.

• Meaning: if we know in advance the number of queries we can fine-tune the algorithm as we wish.

Page 8: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Before we begin - scope

• Assumptions:– No negative cycles

• Inputs:– Directed Weighted Graph G=(V,E,w)– Weights are –M,…0,…,M

• Outputs:– Data structure such that – given any two nodes –

produces the shortest distance between them (and not the path itself) – with high probability.

Page 9: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Matrix Multiplication• The matrix product C=AB, where A is an

matrix, B is , and C is matrix, is defined as follows:

• Define: the minimal number of algebraic operations for computing the matrix product.

• Define as the smallest exponent such that

• Theorem by Coppersmith and Winograd:

m

kjkkiji baC

1,,,

lxmmxn lxn

),,( nmlM

),,( tsr )(),,( nOnnnM tsr

376.2

Page 10: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Distance Products• The distance product , where A is an

matrix, B is , and C is matrix, is defined as follows:

• Recall: if W is an n x n matrix of the edge weights of a graph then is the distance matrix of the graph.

• Lemma by Alon: can be computed almost as fast as “regular” matrix multiplication:

jkkimkji baC ,,1, min

lxmmxn lxn

nW

BAC

BAC

),,( tsrMnO

Page 11: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

State-of-the-art APSP• Randomized algorithm by Zwick that runs in

time• Intuition:

– Computation of all short paths is intensive.– BUT: long paths are made up of short paths: once

we pay the initial price we can leverage this work to compute longer paths with less effort.

• Strategy: Giving up on certainty - with a small number of distance updates we can be almost sure that any long-enough path has at least one representative that is updated.

58.268.0 nMO

Page 12: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Basic Operations• Truncation

– Replace any entry larger than t with

• Selection– Extract from D the elements whose row indices are

in A, and column indices are in B.

• Min-Assignment– Assign to each element the smallest between the

two corresponding elements of D and D‘.

tD

],[ BAD

],[],[ 'min BADBAD

Page 13: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Pseudo-code• Simply sample nodes and multiply decimated

matrices…

endfor

VBDBVD

nnVsampleB

ntolfor

WD

nV

W

sMsM

l

nxn

],[*],[D

)2/3/()ln(9,

log 1

,...,2,1

M),shortpath( algorithm

min

2/3

Page 14: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

On matrices and nodes…

• Column-decimated matrix ],[ BVD

D

],[ BVD

Distance between any two nodes

Shortest directed path from any node to any

node in B

Page 15: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

On matrices and nodes…(2)

• Row-decimated matrix ],[ VBD

],[ VBD

Distance between any two nodes

Shortest directed path from any node in B to

any node

Page 16: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

What do we prove?

• Lemma: if there is a shortest path between nodes i and j in G that uses at most edges, then after the -th iteration of the algorithm, with high probability we have

• Meaning: at each iteration we update with high probability all the paths in the graph of a certain length. This serves as a basis for the next iteration.

jiji d ,, l

l2/3

Page 17: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Proof Outline

• By Induction:– Base case: easy – the input W contains all paths of

length – Induction step:

• Suppose that the claim holds for and show that it also holds for

• Take any two nodes that their shortest distance is at least . The -th iteration matrix product will (almost certainly) plug in their shortest distance at location (i,j) of D.

12/3 0

1ll

12/3 l l

Page 18: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Why?

• Set

• The path p from i to j is at least 2s/3.

• This divides p into three subsections:– Left – at most s/3– Right – at most s/3– Middle – exactly s/3

ls 2/3

i i’ k j’ j

3/s3/s 3/s

Page 19: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

The Details

• The left and right “thirds” - help attain the induction step.– The path p(i,k) and p(k,j) are short enough – at most 2s/3

good for previous step:

• The middle “third” – ensures the fault probability is low enough.– Prob(no k is selected) =

– Probability still goes to 0 (as n tends to infinity) after computation of

• entries• iterations

1l

3

3/1ln9

1ns

ns

2n nO log

Page 20: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

So…• Assuming all previous steps were good enough:

– With high probability each long-enough path has a representative in B

– The update of the D using the product

plugs in the correct result.

• Note that:– Each element is first limited to s*M– This is necessary for the fast-matrix-multiplication

algorithm

],[*],[ VBDBVD

Page 21: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Complexity

• Where does the trick hide?– The matrix alphabet increases linearly with iteration

number

– The product size decreases with iteration number

• For each iteration :– Alphabet size: s*M

– Product complexity: , where

– Total:

• Disregarding the log function, and optimizing between fast and naïve matrix products we get:

lsl 2/3,

1,,1 r snnr /

)( 58.268.0 nMO

1,,111,,1 rrr MnnOsMnO

Page 22: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Fast Product versus Naive

)( 2 rnO

)( )1,,1(1 rrnO

*assuming small M

58.2

Page 23: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Complexity Behavior• For a given matrix alphabet M, we find the

cross-over point between the matrix algorithms.

• For high r (>M-dependent threshold) we use FMM– Complexity dependent on M

• For low r (<threshold) we use naïve multiplication– Complexity not dependent on M

• Q: How does complexity change over the iteration number?

Page 24: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Pre-processing algorithm

• Motivation:– We rarely query all node-pairs

• Strategy:– Replace the costly matrix product

with 2 smaller products:1.

2.

– Generate data structure such that each query costs only

)( 2nO

],[*],[ VBDBVD

],[*],[ BBDBVD

)( 2nO)(nO

],[*],[ VBDBBD

Page 25: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Starting with the query…

• Pseudo-code:

• What is a sufficient trait of D, such that the returned value will be, with high probability

• Answer: with high probability, a node k on the path from i to j should have:

jVDViD

Dnxn,*,return

j)i,,dist( algorithm

),( ji

),(, kid ki

),(, jkd jk

Page 26: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Preprocessing algorithm

endfor

VBDBBD

VBDBVD

nnBsampleB

ntolfor

WDVB

nV

W

sMsM

sMsM

l

nxn

],[*],[V]D[B,

],[*],[B]D[V,

)2/3/()ln(9,

log 1

,

,...,2,1

M),shortpath( algorithm

min

min

2/3

Page 27: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

New matrix type• Row&Column-decimated matrix ],[ BBD

D

],[ BBD

Query data structure for any two nodes

Query data-structure for any 2 nodes in B

Page 28: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

What do we prove?

• Lemma 4.1: If or , and there is a shortest path from i to j in G that uses at most edges, then after the -th iteration of the preprocessing algorithm, with high probability we have .

• Meaning: D has the necessary trait: for any path p, if we iterate long enough, then with high probability, for at least one node k (in p(i,j)) the entries d(i,k), d(k,j) will contain shortest paths. Hence, “query” will return the correct result.

lBi lBj l2/3

l

jijid ,,

Page 29: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Proof Outline - preprocess

• By Induction:– Base case: easy – B=V, and the input W contains

all paths of length . – Induction step:

• Suppose that the claim holds for and show that it also holds for

• Take any two nodes that their shortest distance is at most . The l-th iteration matrix products (2) will (almost certainly) plug in their shortest distance at location (i,j) of D provided that EITHER or

.

1)2/3( 0

1ll

l2/3

lBilBj

Page 30: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Why?

• Set

• The path p from i to j is at least 2s/3.

• This divides p into three subsections:– Left – at most s/3– Right – at most s/3– Middle – exactly s/3

ls 2/3

i i’ k j’ j

3/s3/s 3/s

Page 31: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

The Details• Assume that .• With high probability ( ) there will be k in

p(i,j), such that (remember why?)• Both are also in ,since • We therefore attain the induction step:

– The path p(i,k) and p(k,j) are short enough – at most 2s/3 good for previous step.

– The end-points of these paths (k) are in – Therefore their shortest distance is in D– The second product then updates correctly.

(assumption critical here)

lBi

lBk

1lB VBBB ll 01 ...

)( 3nO

1lB

Page 32: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Where’s the catch?

• In APSP, we assure that:– At every iteration l we compute the shortest path

of length at most .– BUT: we had to update all pairs each time

• In the preprocess algorithm, we assure:– At every iteration l, we compute the shortest path

of length at most only for a selected subset.– BUT: this subset covers all possible subsequent

queries, with high probability.

l)2/3(

l)2/3(

2n

Page 33: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Complexity• Matrix product: instead of operations

we only get

• As before, for each iteration , the alphabet size is s*M.

• Total complexity:

• No matrix-product switch here!

lsl 2/3,

1,,1 r

MnOnO

MnOMnnOsMnOr

rrrrrrr

22

)1111,,11,,

1,,2 rr

Page 34: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Performance

• For small M, as long as the number of queries is less than we get better results than APSP.

• For small M:– The algorithm overtakes Goldberg’s algorithm, if

the graph is dense– For a dense-enough graph , we can run

many SSSP queries and still be faster:

58.1158.2 nn

88.138.2~ nOmnOnmO 88.1nm

nmnnm //

Page 35: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

The larger picture• We saw:

– Alg1: heavy pre-processing, light query– Alg2: light pre-processing, heavy query– Alg3: ?

Query-oriented (APSP)

Preprocess- oriented (pre-process)

Page 36: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

The Third Way• Suppose we know in advance the we require

no more than queries.• We use the following:

1. Perform iterations of the APSP algorithm

2. Perform iterations of the pre-process algorithm

3. Take the matrix B from the last step of step 1. The product returns in any shortest-distance query.

rnq

nr 5.1log1

nr 5.1log

rnO }{,},{ jBDBiD

Page 37: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Huh?

• After the first stage D holds all the shortest path of all “short” paths, of lengths at most with high probability.

• When the second starts stage it can be sure that the induction holds for all

• The second stage takes care of the “long” paths, with respect to querying. Meaning:– If the path is long it will have a representative in

one of the second-phase iterations– If it is too-short – it will fall under the jurisdiction

of the first stage.

rn 1

nrl 5.1log1

Page 38: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Complexity

• The first stage ( updates) costs at most

• The second stage costs only

• The query costs

• For example – if want to answer a distance query in , we can pre-process in time

2n 1,,11 rrMnO

1,,1 rrrMnO

rnO

4/3nO 5.2MnO

Page 39: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Q&A (I ask - you answer)

• Q: Why couldn’t we sample B in the query step of Alg2 – the one that initially costs O(n)?

• A: Because if the path is too short – we will have no guarantee that it will have a representative in B. Alg3 solves this because short distances are computed rigorously.

• Conclusion: the less we sample out of V when we query, the more steps we need to run APSP to begin with.

Page 40: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Final Procedure

• Given q queries, determine the query complexity using .

• This assumes M is small enough so that we use fast product. Otherwise compare to

• Execute alg3 using steps of APSP and steps of pre-process

• Query all q queries.

1,,11 rrr Mnqn

qnr rn 2

nr 5.1log1 nr 5.1log

Page 41: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Summary

• For the problem we defined: directed graph, with integer weights, whose absolute value is at most M, we have seen:– Alg1: State-of-the-art APSP in– Alg2: State-of-the-art SSSP in– Alg3: A method to calibrate between the two, for a

known number of queries.

58.268.0 nMO

38.2MnO

Page 42: Answering Distance Queries in directed graphs using fast matrix multiplication Seminar in Algorithms Prof. Haim Kaplan Lecture by Lior Eldar 1/07/2007

Thank You!