GraphLab Project

graphlab.plugins.main.core
Class AlgorithmUtils

java.lang.Object
  extended by graphlab.plugins.main.core.AlgorithmUtils

public class AlgorithmUtils
extends java.lang.Object

Just some methods helping you to write Graph Algorithms easier,

See Also:
LibraryUtils

Nested Class Summary
static interface AlgorithmUtils.BFSListener<Vertex extends BaseVertex>
           
 
Field Summary
static int Max_Int
           
 
Constructor Summary
AlgorithmUtils()
           
 
Method Summary
static
<Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>>
void
BFS(BaseGraph<Vertex,Edge> unRootedTree, AlgorithmUtils.BFSListener<Vertex> listener)
          performs a full BFS on graph, it selects the vertices with minimum degrees as the roots of the resulting forest
static
<Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>>
java.util.ArrayList<Vertex>
BFS(BaseGraph<Vertex,Edge> unRootedTree, Vertex treeRoot, AlgorithmUtils.BFSListener<Vertex> listener)
          performs a bfs on the given root, this method changes vertex marks, and also marked vertices will not be traversed
static
<Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>>
java.util.ArrayList<Vertex>
BFSOrder(BaseGraph<Vertex,Edge> unRootedTree, Vertex treeRoot)
          gets the vertices in the order of AlgorithmUtils.getSubTree()
static
<Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>>
void
BFSrun(BaseGraph<Vertex,Edge> unRootedTree, Vertex treeRoot, AlgorithmUtils.BFSListener<Vertex> listener)
          runs a BFS on graph, starting the given vertex as the root
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
void
clearVertexMarks(BaseGraph<VertexType,EdgeType> g)
          clears all vertex marks
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
void
dfs(BaseGraph<VertexType,EdgeType> g, int node, java.util.ArrayList visit, int[] parent)
          runs a dfs and fills visit and parent, visit is the visiting order of vertices and parent[i] is the id of i'th vertex parent
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
java.util.ArrayList<java.util.ArrayList<java.lang.Integer>>
getAdjList(BaseGraph<VertexType,EdgeType> g)
          Deprecated. use BaseGraph.getEdgeArray instead
static double getAngle(GraphPoint p1, GraphPoint p2)
           
static double getAngle(GraphPoint rootp, GraphPoint v1p, GraphPoint v2p)
          returns the angle between 3 points
static double getAngle(VertexModel root, VertexModel v1, VertexModel v2)
          returns the angle between 3 vertices in graphical world!
static java.awt.geom.Rectangle2D.Double getBoundingRegion(java.util.Collection<VertexModel> vertices)
           
static GraphPoint getCenter(java.util.Collection<VertexModel> V)
           
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
int
getDegree(BaseGraph<VertexType,EdgeType> bg, int node)
          Deprecated.  
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
java.util.ArrayList<java.lang.Integer>
getDegreesList(BaseGraph<VertexType,EdgeType> g)
          returns the vertex degrees as a list, sorted by vertex ids
static double getDistance(GraphPoint p1, GraphPoint p2)
          returns the distance between two points
static double getDistance(VertexModel v1, VertexModel v2)
          returns the distance between two vertices in pixels, (in graphics not the path length between them)
static double getLength(double dx, double dy)
          returns the length of the given vector
static GraphPoint getMiddlePoint(GraphPoint p1, GraphPoint p2)
           
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
java.util.ArrayList<VertexType>
getNeighbors(BaseGraph<VertexType,EdgeType> g, VertexType source)
          Deprecated.  
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
java.util.ArrayList<VertexType>
getNeighbors2(BaseGraph<VertexType,EdgeType> g, VertexType source)
          Deprecated.  
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
VertexType
getParent(BaseGraph<VertexType,EdgeType> g, VertexType treeRoot, VertexType v)
          returns the parent of v, if ve DFS on parent
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
Path<VertexType>
getPath(BaseGraph<VertexType,EdgeType> g, VertexType source, VertexType dest)
          returns a path from source to target path.get(0) = dest
static
<Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>>
Vertex
getRoot(BaseGraph<Vertex,Edge> g, Vertex v)
          returns the root which is assigned to each vertex it is the minimum id vertex in the corresponding component of vertex
static
<Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>>
java.util.ArrayList<Vertex>
getSubTree(BaseGraph<Vertex,Edge> tree, Vertex treeRoot, Vertex subTreeRoot)
          returns the subtree rooted by subTreeRoot in the rooted tree tree with the root treeRoot the vertices are ordered by their distances to subTreeRoot the exact distance is placed in v.getProp().obj as an Integer, starting distance is 0 which is subTreeRoot
static int getTotalDegree(BaseGraph g, BaseVertex v)
          retunrs the degree of vertex (indegree + outdegree)
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
boolean
isCompleteGraph(BaseGraph<VertexType,EdgeType> g)
          determines wether g is complete or not
static
<VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>>
boolean
isConnected(BaseGraph<VertexType,EdgeType> g)
          determines wether g is connected or not
static void move(VertexModel v, double dx, double dy)
          moves the vertex relative to its current position
static GraphPoint normalize(GraphPoint vector)
           
static void resetVertexColors(BaseGraph<BaseVertex,BaseEdge<BaseVertex>> g)
          sets all vertex colors to 0.
static void resetVertexMarks(BaseGraph<BaseVertex,BaseEdge<BaseVertex>> g)
          sets all vertex marks to false
static void setLocation(VertexModel v, GraphPoint center, double radius, double ang)
          locations v in a r-teta cordination
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

Max_Int

public static final int Max_Int
See Also:
Constant Field Values
Constructor Detail

AlgorithmUtils

public AlgorithmUtils()
Method Detail

resetVertexColors

public static void resetVertexColors(BaseGraph<BaseVertex,BaseEdge<BaseVertex>> g)
sets all vertex colors to 0.


resetVertexMarks

public static void resetVertexMarks(BaseGraph<BaseVertex,BaseEdge<BaseVertex>> g)
sets all vertex marks to false


isConnected

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> boolean isConnected(BaseGraph<VertexType,EdgeType> g)
determines wether g is connected or not


isCompleteGraph

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> boolean isCompleteGraph(BaseGraph<VertexType,EdgeType> g)
determines wether g is complete or not


getAdjList

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> java.util.ArrayList<java.util.ArrayList<java.lang.Integer>> getAdjList(BaseGraph<VertexType,EdgeType> g)
Deprecated. use BaseGraph.getEdgeArray instead

returns the adjacency list of g.


getDegree

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> int getDegree(BaseGraph<VertexType,EdgeType> bg,
                                                                                                  int node)
Deprecated. 

returns the degree of the node with the id

See Also:
BaseGraph.getDegree(graphlab.library.BaseVertex)

getNeighbors

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> java.util.ArrayList<VertexType> getNeighbors(BaseGraph<VertexType,EdgeType> g,
                                                                                                                                 VertexType source)
Deprecated. 

returns all neighbors of the given vertex

See Also:
BaseGraph.getNeighbors(graphlab.library.BaseVertex)

getNeighbors2

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> java.util.ArrayList<VertexType> getNeighbors2(BaseGraph<VertexType,EdgeType> g,
                                                                                                                                  VertexType source)
Deprecated. 

returns all neighbors of the given vertex

See Also:
BaseGraph.getNeighbors(graphlab.library.BaseVertex)

getPath

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> Path<VertexType> getPath(BaseGraph<VertexType,EdgeType> g,
                                                                                                             VertexType source,
                                                                                                             VertexType dest)
returns a path from source to target path.get(0) = dest


getParent

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> VertexType getParent(BaseGraph<VertexType,EdgeType> g,
                                                                                                         VertexType treeRoot,
                                                                                                         VertexType v)
returns the parent of v, if ve DFS on parent


clearVertexMarks

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> void clearVertexMarks(BaseGraph<VertexType,EdgeType> g)
clears all vertex marks


getSubTree

public static <Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>> java.util.ArrayList<Vertex> getSubTree(BaseGraph<Vertex,Edge> tree,
                                                                                                               Vertex treeRoot,
                                                                                                               Vertex subTreeRoot)
returns the subtree rooted by subTreeRoot in the rooted tree tree with the root treeRoot the vertices are ordered by their distances to subTreeRoot the exact distance is placed in v.getProp().obj as an Integer, starting distance is 0 which is subTreeRoot


BFSOrder

public static <Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>> java.util.ArrayList<Vertex> BFSOrder(BaseGraph<Vertex,Edge> unRootedTree,
                                                                                                             Vertex treeRoot)
gets the vertices in the order of AlgorithmUtils.getSubTree()


BFSrun

public static <Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>> void BFSrun(BaseGraph<Vertex,Edge> unRootedTree,
                                                                                    Vertex treeRoot,
                                                                                    AlgorithmUtils.BFSListener<Vertex> listener)
runs a BFS on graph, starting the given vertex as the root


BFS

public static <Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>> void BFS(BaseGraph<Vertex,Edge> unRootedTree,
                                                                                 AlgorithmUtils.BFSListener<Vertex> listener)
performs a full BFS on graph, it selects the vertices with minimum degrees as the roots of the resulting forest

Parameters:
unRootedTree -
listener -

BFS

public static <Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>> java.util.ArrayList<Vertex> BFS(BaseGraph<Vertex,Edge> unRootedTree,
                                                                                                        Vertex treeRoot,
                                                                                                        AlgorithmUtils.BFSListener<Vertex> listener)
performs a bfs on the given root, this method changes vertex marks, and also marked vertices will not be traversed

Parameters:
unRootedTree -
treeRoot -
listener -
Returns:

dfs

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> void dfs(BaseGraph<VertexType,EdgeType> g,
                                                                                             int node,
                                                                                             java.util.ArrayList visit,
                                                                                             int[] parent)
runs a dfs and fills visit and parent, visit is the visiting order of vertices and parent[i] is the id of i'th vertex parent


getTotalDegree

public static int getTotalDegree(BaseGraph g,
                                 BaseVertex v)
retunrs the degree of vertex (indegree + outdegree)


getRoot

public static <Vertex extends BaseVertex,Edge extends BaseEdge<Vertex>> Vertex getRoot(BaseGraph<Vertex,Edge> g,
                                                                                       Vertex v)
returns the root which is assigned to each vertex it is the minimum id vertex in the corresponding component of vertex


getAngle

public static double getAngle(VertexModel root,
                              VertexModel v1,
                              VertexModel v2)
returns the angle between 3 vertices in graphical world!


getAngle

public static double getAngle(GraphPoint rootp,
                              GraphPoint v1p,
                              GraphPoint v2p)
returns the angle between 3 points


getLength

public static double getLength(double dx,
                               double dy)
returns the length of the given vector


move

public static void move(VertexModel v,
                        double dx,
                        double dy)
moves the vertex relative to its current position


getDistance

public static double getDistance(VertexModel v1,
                                 VertexModel v2)
returns the distance between two vertices in pixels, (in graphics not the path length between them)


getDistance

public static double getDistance(GraphPoint p1,
                                 GraphPoint p2)
returns the distance between two points


getAngle

public static double getAngle(GraphPoint p1,
                              GraphPoint p2)
Returns:
the angle between vector p2-p1 and X-Axis

setLocation

public static void setLocation(VertexModel v,
                               GraphPoint center,
                               double radius,
                               double ang)
locations v in a r-teta cordination


getBoundingRegion

public static java.awt.geom.Rectangle2D.Double getBoundingRegion(java.util.Collection<VertexModel> vertices)
Returns:
the bounding rectangle arround vertices

getCenter

public static GraphPoint getCenter(java.util.Collection<VertexModel> V)

getDegreesList

public static <VertexType extends BaseVertex,EdgeType extends BaseEdge<VertexType>> java.util.ArrayList<java.lang.Integer> getDegreesList(BaseGraph<VertexType,EdgeType> g)
returns the vertex degrees as a list, sorted by vertex ids


getMiddlePoint

public static GraphPoint getMiddlePoint(GraphPoint p1,
                                        GraphPoint p2)
Parameters:
p1 -
p2 -
Returns:
a point whose x and y are average of the given graph points.

normalize

public static GraphPoint normalize(GraphPoint vector)

GraphLab Project