Mengambil sampel secara efisien jalur

14

Mari menjadi grafik, dan biarkan dan dua simpul dari . Bisakah kita secara efisien mencicipi terpendekGstGs - t secara seragam dan independen secara acak dari rangkaian semua jalur terpendek antara s dan t ? Untuk kesederhanaan, kita dapat menganggap G sederhana, tidak terarah dan tidak berbobot.

Bahkan di banyak grafik terbatas, jumlah jalur terpendek antara s dan t dapat eksponensial dalam ukuran G . Oleh karena itu, kita tentu ingin menghindari sebenarnya menghitung semua jalur s - terpendek t. Saya tidak tahu tentang kasus umum, tetapi bagi saya tampaknya kita dapat mencapai ini untuk beberapa kelas grafik khusus.

Ini terasa seperti sesuatu yang seseorang harus pertimbangkan sebelumnya. Apakah ada penelitian yang ada tentang ini, atau apakah ini sebenarnya mudah dilakukan bahkan untuk grafik umum?

Juho
sumber
Pertanyaan bagus Juho. Sambil mempertimbangkan jawaban, apa yang Anda benar-benar pahami dengan "mengambil sampel jalur seragam secara acak"? Jika cukup untuk s dan t untuk diambil secara acak pertanyaannya sepele jadi saya kira Anda maksudkan bahwa semua node di jalur terpendek muncul dengan frekuensi (mis., Probabilitas) yang mengikuti distribusi seragam. Atau adakah definisi lain? Khususnya, untuk grafik bipartit, pertanyaan Anda tampaknya sangat mudah, bukan?
Carlos Linares López
1
@ CarlosLinaresLópez Pertimbangkan untuk mengatakan grafik berlian , dan katakanlah s ada di sisi kanan "tepi vertikal", dan t ada di sisi kiri. Sekarang ada 2 jalur terpendek antara s dan t . Algoritme harus kembali dengan probabilitas yang sama salah satu dari dua jalur ini. Jadi s dan t tidak "diambil secara acak", tetapi mereka diberikan sebagai input. Apakah itu menjelaskan? Dalam hal ini, saya tidak yakin apakah masalahnya sangat mudah untuk grafik bipartit.
Juho
1
@ CarlosLinaresLópez Dengan kata lain, kita diberi grafik , dan dua simpul s , t V ( G ) . Misalkan S adalah himpunan semua jalur terpendek antara dan . Keluarkan elemen secara seragam secara acak. Gs,tV(G)SstS
Juho

Jawaban:

6

Saya tidak 100% yakin jawaban ini benar, tetapi begini:

Saya pikir Anda dapat mengurangi ini untuk acak sembarang-jalur, dari , dalam DAG dengan satu sumber dan satu wastafel.st

Diberikan grafikG

  1. Membuat digraf kosong baru, .H
  2. Pertama: menjalankan BFS bagian dari Dijkstra terpendek-jalan, mulai dari , menandai semua node dengan jarak-dari- terpendek mereka .ss
  3. Misalkan menjadi jarak minimum dari ; yang kita tahu dari langkah BFS dari algoritma jalur terpendek Dijkstra.d(s,v)sv
  4. Kemudian lakukan langkah berikutnya dari algoritma jalur terpendek Dijkstra, dapatkan jalur terpendek, simpan di (dengan mundur dari ke ).pts
  5. Sekarang mulai loop berikut; perluasan dalam komentar, dan di bawah ini:
    • q0={t}
    • Sementaraq0
      • q1=
      • For uq0
        • So we want to find all possible next nodes for this shortest-subpath from tu
        • For all edge(u,v)G such that d(s,v)<d(s,u)
          • v is a neighboring node, with less d(s,) (it will be 1 less)
          • Therefore, tuv is possible subpath in a shortest path.
          • Put vH,di-edge(u,v)H
          • Now we need to check v's lesser-neighbors next turn.
          • Put vq1
      • Set q0 to q1:
        • q0q1

Essentially, I am collecting all possible nodes that can be used in the shortest-path, and placing them in H.

More on how this works:

Dijkstra's shortest-path algorithm works by first running a BFS, and marking all the nodes vG with their-shortest paths from sv. The next step is to go back from ts, and follow the least-neighboring nodes back.

The thing is, here you can choose any of the least neighboring nodes. What I do here is collect all the least-neighboring nodes each step, which means I account for all the shortest-paths.

Now you quickly think, but hey, why is enumerating them exponential, but my way is not?

The answer is, because I use a set to avoid adding the same nodes twice, I avoid recalculating this for each possible path.

Now we have a DAG that we can traverse in any way from ts, and obtain a shortest-reversed-path from st. The graph should have t as the only source, and s as the only sink.


If the above is correct, then I think we can take this a step further and solve the problem as follows.

Give each node in the DAG a node-weight; the node-weight will be the number of paths from from that node to s. Let us call this w(v).

You can compute these quickly, see Algorithm that finds the number of simple paths from s to t in G.

Once we have the node-weight, we can uniformly pick a path by:

  • Layout the DAG as a level-structure (for visualization)
  • At each level, choose an arbitrary ordering between the nodes ie. a notion of "left to right".
  • Traversing down the DAG: at each step i, i[1,|p|] (where || means size-of, in this case, the length of the shortest-path):
    • Let ui be the current node (starting at t)
    • Add up all the weights of the children of ui, and using an RNG, choose one child node, vi, uniformly between the weighted children.
    • Set ui+1=vi, and go to the next step
Realz Slaw
sumber
The level-structure, and notion of left-to-right were part of my initial attempt to simply generate r[0,w(t)), and choose a path that way, but I didn't figure that out, so you can safely ignore them.
Realz Slaw
1
This answer looks great! I love the ideas! I tried to write it out in a slightly different way (in my answer), as a test of my understanding. In any case, I just wanted to share my appreciation for this lovely answer!
D.W.
5

Here is a solution based upon the ideas in Realz Slaw's answer. It is basically a re-exposition of his ideas that might be clearer or easier to follow. The plan is that we will proceed in two steps:

  1. First, we will build a graph S with the following property: any path from s to t in S is a shortest path from s to t in G, and every shortest path from s to t in G is also present in S. Thus, S contains exactly the shortest paths in G: all the shortest paths, and nothing more. As it happens, S will be a DAG.

  2. Next, we will sample uniformly at random from all paths from s to t in S.

This approaches generalizes to an arbitrary directed graph G, as long as all edges have positive weight, so I'll explain my algorithm in those terms. Let w(u,v) denote the weight on the edge uv. (This generalizes the problem statement you gave. If you have an unweighted graph, just assume every edge has weight 1. If you have an undirected graph, treat each undirected edge (u,v) as the two directed edges uv and vu.)


Step 1: extract S. Run a single-source shortest-paths algorithm (e.g., Dijkstra's algorithm) on G, starting from source s. For each vertex v in G, let d(s,v) denote the distance from s to v.

Now define the graph S as follows. It consists of every edge uv such that (1) uv is an edge in G, and (2) d(s,v)=d(s,u)+w(u,v).

The graph S has some convenient properties:

  • Every shortest path from s to t in G exists as a path in S: a shortest path s=v0,v1,v2,,vk=t in G has the property that d(s,vi+1)=d(s,vi)+w(vi,vi+1), so the edge vivi+1 is present in S.

  • Every path in S from s to t is a shortest path in G. In particular, consider any path in S from s to t, say s=v0,v1,v2,,vk=t. Its length is given by the sum of the weights of its edges, namely i=1kw(vi1,vi), but by the definition of S, this sum is i=1k(d(s,vi)d(s,vi1), which telescopes to d(s,t)d(s,s)=d(s,t). Therefore, this path is a shortest path from s to t in G.

  • Finally, the absence of zero-weight edges in G implies that S is a dag.

Step 2: sample a random path. Now we can throw away the weights on the edges in S, and sample a random path from s to t in S.

To help with this, we will do a precomputation to compute n(v) for each vertex v in S, where n(v) counts the number of distinct paths from v to t. This precomputation can be done in linear time by scanning the vertices of S in topologically sorted order, using the following recurrence relation:

n(v)=wsucc(v)n(w)

where succ(v) denotes the successors of v, i.e., succ(v)={w:vw is an edge in S}, and where we have the base case n(t)=1.

Next, we use the n() annotation to sample a random path. We first visit node s. Then, we randomly choose one of the successors of s, with successor w weighted by n(w). In other words:

choosesuccessor(v):
    n = 0
    for each w in succ(w):
        n = n + n(w)
    r = a random integer between 0 and n-1
    n = 0
    for each w in succ(w):
        n = n + n(w)
        if r < n:
            return w

To choose a random path, we repeatedly iterate this process: i.e., v0=s, and vi+1= choosesuccessor(vi). The resulting path is the desired path, and it will be sampled uniformly at random from all shortest paths from s to t.

Hopefully this helps you understand Realz Slaw's solution more easily. All credit to Realz Slaw for the beautiful and clean solution to this problem!


The one case this doesn't handle is the case where some edges have weight 0 or negative weight. However, the problem is potentially not well-defined in that case, as you can have infinitely many shortest paths.

D.W.
sumber
Glad you took the time to fully get my answer; I wasn't sure it is correct. Now I am vindicated :D.
Realz Slaw