igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] Random walk from A to B


From: Tamas Nepusz
Subject: Re: [igraph] Random walk from A to B
Date: Wed, 10 Feb 2016 22:11:02 +0100

Hello Nick,

There is no pre-compiled function within the C core, so I would
probably try a Python generator combined with iter():

import random

def random_walk(graph, node):
    neis = graph.neighbors
    while True:
        yield node
        node = random.choice(neis(node))

walk = list(iter(random_walk(graph, start), end))

The trick here is that iter() can take a sentinel value that
terminates the iteration. Here we simply use the end node as the
sentinel value.

The random_walk() function above probably works for undirected graphs.
For directed graphs, you need to ensure that random.choice() does not
freak out if there are no outbound edges to follow from node.

T.


On Wed, Feb 10, 2016 at 7:03 PM, Nick Eubank <address@hidden> wrote:
> Hello All,
>
> Any suggestions on the most efficient way to do a random walk starting at
> node A until the walk reaches node B (or hits a max number of iterations)?
>
> I see I could use `random_walk` and just have it run a long time and then
> look for first arrival at node B, but I don't see a way to terminate the
> random walk when B is reached.
>
> (python-igraph solutions preferred if possible! I'll code it by hand if
> needed, but I have a huge network (10 million nodes), so just want to see if
> there are any pre-compiled functions!)
>
> Thanks!
>
> Nick
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/igraph-help
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]