org.graphstream.algorithm.randomWalk
Class RandomWalk

java.lang.Object
  extended by org.graphstream.stream.SinkAdapter
      extended by org.graphstream.algorithm.randomWalk.RandomWalk
All Implemented Interfaces:
Algorithm, DynamicAlgorithm, org.graphstream.stream.AttributeSink, org.graphstream.stream.ElementSink, org.graphstream.stream.Sink

public class RandomWalk
extends org.graphstream.stream.SinkAdapter
implements DynamicAlgorithm

A random walk on a graph.

Idea

This algorithm create a given number of entities first associated with random nodes in the graph. Then by turns, each entity chooses an edge at random and crosses it. This is iterated a given number of turns. Each time an entity crosses an edge, a count is incremented on it and each time it arrives on a node a count is counted on it.

You can override the entity class to provide your own behaviour for entity movement.

Counts on edges and nodes

If the algorithm was run for an infinite number of turns, each counter would have the same value. However we can choose to stop the algorithm when needed. Furthermore the algorithm can be biased by providing each entity with a memory of the already crossed edges. It can avoid these edges when choosing at random its next edge.

When an entity has no edge to choose (either because of its memory or because it reached a node that is only reachable via a one directed edge), the entity will jump randomly on another node.

When the number of turns awaited is reached, one can observe the counts on each edge and node. Edges and nodes that are very attractive in terms of topology should have a more important count than others.

This algorithm does not cope well with dynamic graphs. You can however improve this by using evaporation. When evaporation is activated, at each turn, the node and edge counts are multiplied by a number between 0 and 1. Therefore each edge or node count must be constantly updated by entities leading to a value that stabilizes in time.

The basic tabu entity

At each step, the default entities move from their current node to another via an edge randomly chosen. This is done in the Entity.step() method.

This method makes a list of all leaving edges of the current node. If the node has no leaving edge, the entity jumps to another randomly chosen node. Then an edge is chosen at random in the list of leaving edges. The edge is chosen uniformly if there are no weights on the edges, else, an edge with an higher weight has more chances to be chosen than an edge with a lower weight.

When crossed, if the memory is larger than 0, the edge crossed is remembered so that the entity will not choose it anew until it crosses as many edges as the memory size.

Usage

With the default entities, you can make a node entirely tabu by putting the ``tabu`` attribute on it. No entity will traverse an edge that leads to such a node.

You can change the default entity class either by overriding the createEntity() method or by changing the entity class name using setEntityClass(String).

If the edges have weights, the entities can use them to favour edges with higher weights when randomly choosing them. By default the weights are searched on edges using the ``weight`` attribute. However you can override this using setWeightAttribute(String) method.

If you choose to have evaporation on edge counts at each turn, you can set it using setEvaporation(double). The evaporation is a number between 0 and 1. If set to 1 (the default), the counts are not modified, else the counts are multiplied by the evaporation at each turn.

To compute a turn, use the compute() method. This will move each entity from one node to another.

Once computed each edge and node will have an attribute ``passes`` stored on it containing the number of passage of an entity. You can change the name of this attribute using setPassesAttribute(String). After each computation of a turn, you can obtain the edge and nodes counts using either the passes attribute, or the utility methods getPasses(Node) and getPasses(Edge).

You can count only the passes on the nodes or edges using the two methods computeEdgesPasses(boolean) and computeNodePasses(boolean).

As some entities may have jumped from their node to another one chosen randomly, you can obtain the number of entities that jumped using getJumpCount().

Complexity

The complexity, at each turn is O(n) with n the number of entities.

Example

Here is how to compute a simple pass count for 1000 steps:
 Graph graph = new MultiGraph("random walk");
 RandomWalk rwalk = new RandomWalk();
 // Populate the graph.
 rwalk.setEntityCount(graph.getNodeCount()/2);
 rwalk.init(graph);
 for(int i=0; i<1000; i++) {
                rwalk.compute();
 }
 rwalk.terminate();
 for(Edge edge: graph.getEachEdge()) {
                System.out.println("Edge %s counts %f%n", edge.getId(), rwalk.getPasses(edge));
 }
 


Nested Class Summary
 class RandomWalk.Context
           
 
Constructor Summary
RandomWalk()
          New random walk with a new random seed (based on time), with an entity memory set to 10 nodes (tabu list), with an attributes to store passes named "passes" and no weight attribute.
RandomWalk(long randomSeed)
          New random walk with a given random seed, with an entity memory set to 10 nodes (tabu list), with an attributes to store passes named "passes" and no weight attribute.
 
Method Summary
 void compute()
          Execute one step of the algorithm.
 void computeEdgesPasses(boolean on)
          Activate or not the counts on edges when entities cross thems.
 void computeNodePasses(boolean on)
          Activate or not the counts on nodes when entities cross thems.
 Entity createEntity()
          Create an entity.
 void edgeAdded(String graphId, long timeId, String edgeId, String fromNodeId, String toNodeId, boolean directed)
           
 ArrayList<org.graphstream.graph.Edge> findTheMostUsedEdges()
          Sort all edges by their "passes" attribute and return the array of sorted edges.
 ArrayList<org.graphstream.graph.Node> findTheMostUsedNodes()
          Sort all nodes by their "passes" attribute and return the array of sorted nodes.
 int getEntityCount()
          Number of entities.
 double getEvaporation()
          The evaporation value.
 int getGoCount()
           
 int getJumpCount()
          Number of entities that jumped instead of traversing an edge at last step.
 double getJumpRatio()
          Ratio of entities that executed a jump instead of traversing an edge at last step.
 double getPasses(org.graphstream.graph.Edge edge)
          The number of entity passage on the given edge.
 double getPasses(org.graphstream.graph.Node node)
          The number of entity passage on the given node.
 String getPassesAttribute()
          The name of the attribute where the number of entities passes are stored (for edges and nodes).
 long getRandomSeed()
          The random seed used.
 int getWaitCount()
           
 void init(org.graphstream.graph.Graph graph)
          Initialize the algorithm for a given graph with a given entity count.
 void nodeAdded(String graphId, long timeId, String nodeId)
           
 void setEntityClass(String name)
          Set the name of the entity class to use.
 void setEntityCount(int entityCount)
          Set the number of entities which will be created at the algorithm initialization.
 void setEntityMemory(int size)
          Set the entity memory in number of nodes remembered.
 void setEvaporation(double evaporation)
          Set the evaporation of edge counts.
 void setPassesAttribute(String name)
          Set the name of the attribute used to store the number of passes of each entity on each edge or node.
 void setWeightAttribute(String name)
          The name of the attribute used to fetch edges importance.
 void terminate()
          End the algorithm by removing any listener on the graph and releasing memory.
 
Methods inherited from class org.graphstream.stream.SinkAdapter
edgeAttributeAdded, edgeAttributeChanged, edgeAttributeRemoved, edgeRemoved, graphAttributeAdded, graphAttributeChanged, graphAttributeRemoved, graphCleared, nodeAttributeAdded, nodeAttributeChanged, nodeAttributeRemoved, nodeRemoved, stepBegins
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RandomWalk

public RandomWalk()
New random walk with a new random seed (based on time), with an entity memory set to 10 nodes (tabu list), with an attributes to store passes named "passes" and no weight attribute.


RandomWalk

public RandomWalk(long randomSeed)
New random walk with a given random seed, with an entity memory set to 10 nodes (tabu list), with an attributes to store passes named "passes" and no weight attribute.

Parameters:
randomSeed - The random seed.
Method Detail

getPassesAttribute

public String getPassesAttribute()
The name of the attribute where the number of entities passes are stored (for edges and nodes).

Returns:
A string representing the attribute name for entity passes.

setEntityClass

public void setEntityClass(String name)
Set the name of the entity class to use. If set to null, the default entity class will be used (RandomWalk.TabuEntity).

Parameters:
name - The name of the entity class to use.

setEntityMemory

public void setEntityMemory(int size)
Set the entity memory in number of nodes remembered. This memory is used as a tabu list, that is a set of nodes not to cross.

Parameters:
size - The memory size, 0 is a valid size to disable the tabu list.

setEvaporation

public void setEvaporation(double evaporation)
Set the evaporation of edge counts. This is a number between 0 and 1. If less than 1, at each turn, each edge count is multiplied by this factor. The use of evaporation allows to stabilize the counts.

Parameters:
evaporation - A number between 0 and 1.

getEvaporation

public double getEvaporation()
The evaporation value.

Returns:
The evaporation.

getRandomSeed

public long getRandomSeed()
The random seed used.

Returns:
A long integer containing the random seed.

getEntityCount

public int getEntityCount()
Number of entities.

Returns:
The number of entities.

getJumpCount

public int getJumpCount()
Number of entities that jumped instead of traversing an edge at last step. An entity executes a jump when it is blocked in a dead end (either a real one, or because of its tabu list).

Returns:
The jump count.

getWaitCount

public int getWaitCount()

getGoCount

public int getGoCount()

getJumpRatio

public double getJumpRatio()
Ratio of entities that executed a jump instead of traversing an edge at last step. An entity executes a jump when it is blocked in a dead end (either a real one, or because of its tabu list).

Returns:
The jump ratio (in [0-1]).

setWeightAttribute

public void setWeightAttribute(String name)
The name of the attribute used to fetch edges importance.

Parameters:
name - A string giving the weight name.

setPassesAttribute

public void setPassesAttribute(String name)
Set the name of the attribute used to store the number of passes of each entity on each edge or node.

Parameters:
name - A string giving the passes name.

getPasses

public double getPasses(org.graphstream.graph.Edge edge)
The number of entity passage on the given edge.

Parameters:
edge - The edge to look at.
Returns:
The number of passes on the edge.

getPasses

public double getPasses(org.graphstream.graph.Node node)
The number of entity passage on the given node.

Parameters:
node - The node to look at.
Returns:
The number of passes on the node.

setEntityCount

public void setEntityCount(int entityCount)
Set the number of entities which will be created at the algorithm initialization.

Parameters:
entityCount -

computeEdgesPasses

public void computeEdgesPasses(boolean on)
Activate or not the counts on edges when entities cross thems.

Parameters:
on - If true (the default) the edges passes are counted.

computeNodePasses

public void computeNodePasses(boolean on)
Activate or not the counts on nodes when entities cross thems.

Parameters:
on - If true (the default) the nodes passes are counted.

createEntity

public Entity createEntity()
Create an entity. Override this method to create different kinds of entities or change the entity class name. The default one is the "TabuEntity".

Returns:
The new entity.
See Also:
setEntityClass(String)

init

public void init(org.graphstream.graph.Graph graph)
Initialize the algorithm for a given graph with a given entity count. The entities are created at random locations on the graph.

Specified by:
init in interface Algorithm
Parameters:
graph - The graph to explore.

compute

public void compute()
Execute one step of the algorithm. During one step, each entity choose a next edge to cross, toward a new node. The passes attribute of these edge and node are updated.

Specified by:
compute in interface Algorithm
See Also:
Algorithm.init(Graph)

terminate

public void terminate()
End the algorithm by removing any listener on the graph and releasing memory.

Specified by:
terminate in interface DynamicAlgorithm
See Also:
Algorithm.init(org.graphstream.graph.Graph)

findTheMostUsedEdges

public ArrayList<org.graphstream.graph.Edge> findTheMostUsedEdges()
Sort all edges by their "passes" attribute and return the array of sorted edges.

Returns:
An array with all edges of the graph sorted by their number of entity pass.

findTheMostUsedNodes

public ArrayList<org.graphstream.graph.Node> findTheMostUsedNodes()
Sort all nodes by their "passes" attribute and return the array of sorted nodes.

Returns:
An array with all nodes of the graph sorted by their number of entity pass.

edgeAdded

public void edgeAdded(String graphId,
                      long timeId,
                      String edgeId,
                      String fromNodeId,
                      String toNodeId,
                      boolean directed)
Specified by:
edgeAdded in interface org.graphstream.stream.ElementSink
Overrides:
edgeAdded in class org.graphstream.stream.SinkAdapter

nodeAdded

public void nodeAdded(String graphId,
                      long timeId,
                      String nodeId)
Specified by:
nodeAdded in interface org.graphstream.stream.ElementSink
Overrides:
nodeAdded in class org.graphstream.stream.SinkAdapter


Copyright © 2011. All Rights Reserved.