Class BasicGraph
- Object
-
- BasicGraph
-
- All Implemented Interfaces:
Serializable
,Graph
- Direct Known Subclasses:
BasicDirectedGraph
public class BasicGraph extends Object implements Graph, Serializable
Basic implemenation of Graph.- Author:
- Justin Deoliveira, Refractions Research Inc, jdeolive@refractions.net
- See Also:
Graph
, Serialized Form
-
-
Field Summary
-
Fields inherited from interface Graph
FAIL_QUERY, PASS_AND_CONTINUE, PASS_AND_STOP
-
-
Constructor Summary
Constructors Constructor Description BasicGraph()
Constructs an empty graph with edge and node collections uninitialized.BasicGraph(Collection<Node> nodes, Collection<Edge> edges)
Constructs a graph from a collection of nodes and a collection of edges.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Collection<Edge>
getEdges()
Returns the edges of the graph.Collection<Node>
getNodes()
Returns the nodes of the graph.List<Node>
getNodesOfDegree(int n)
Returns all the nodes in the graph of a specified degree.List<Edge>
getVisitedEdges(boolean visited)
Returns all the edges in the graph that have been marked as visited or non-visited.List<Node>
getVisitedNodes(boolean visited)
Returns all the nodes in the graph that have been marked as visited or non-visited.void
initEdges()
Initializes the edges in the graph by setting all visited flags to false and all visited counts to zero.void
initNodes()
Initializes the nodes in the graph by setting all visited flags to false and all visited counts to zero.List<Edge>
queryEdges(GraphVisitor visitor)
Performs a query against the edges of the graph.List<Node>
queryNodes(GraphVisitor visitor)
Performs a query against the nodes of the graph.void
setEdges(Collection<Edge> edges)
Sets the edge collection for the graph.void
setNodes(Collection<Node> nodes)
Sets the node collection of the graph.String
toString()
Returns the string representation of the graph which is just the string representation of the edge and node collections.void
visitEdges(GraphVisitor visitor)
Applies the visitor pattern to the edges of the graph.void
visitNodes(GraphVisitor visitor)
Applies the visitor pattern to the nodes of the graph.
-
-
-
Constructor Detail
-
BasicGraph
public BasicGraph()
Constructs an empty graph with edge and node collections uninitialized. Mainly for serializability purposes.
-
BasicGraph
public BasicGraph(Collection<Node> nodes, Collection<Edge> edges)
Constructs a graph from a collection of nodes and a collection of edges. The relationships between the nodes (edges) are already assumed to be formed. Only the references to the node and edge collections are copied, not the underlying collections themselves.- Parameters:
nodes
- Collection of nodes to be contained by the graph.edges
- Collection of edges to be contained by the graph.
-
-
Method Detail
-
setNodes
public void setNodes(Collection<Node> nodes)
Sets the node collection of the graph.- Parameters:
nodes
- Collection of Node objects.
-
getNodes
public Collection<Node> getNodes()
Description copied from interface:Graph
Returns the nodes of the graph.- Specified by:
getNodes
in interfaceGraph
- Returns:
- A collection of Node objects.
- See Also:
Graph.getNodes()
-
setEdges
public void setEdges(Collection<Edge> edges)
Sets the edge collection for the graph.- Parameters:
edges
- Collection of Edge objects.
-
getEdges
public Collection<Edge> getEdges()
Description copied from interface:Graph
Returns the edges of the graph.- Specified by:
getEdges
in interfaceGraph
- Returns:
- A collection of Edge objects.
- See Also:
Graph.getEdges()
-
queryNodes
public List<Node> queryNodes(GraphVisitor visitor)
Description copied from interface:Graph
Performs a query against the nodes of the graph. Each Node object contained in the graph is passed to a GraphVisitor to determine if it meets the query criteria.- Specified by:
queryNodes
in interfaceGraph
- Parameters:
visitor
- Determines if node meets query criteria. Returns MEET_AND_CONTINUE to signal that the node meets the query criteria and the query should continue.
Returns MEET_AND_STOP to signal that the node meest the query criteria and the query should stop.
FAIL_QUERY to signal that the node does NOT meet the query criteria.- Returns:
- A collection of nodes that meet the query criteria.
- See Also:
Graph.queryNodes(GraphVisitor)
-
queryEdges
public List<Edge> queryEdges(GraphVisitor visitor)
Description copied from interface:Graph
Performs a query against the edges of the graph. Each Edge object contained in the graph is passed to a GraphVisitor to determine if it meets the query criteria.- Specified by:
queryEdges
in interfaceGraph
- Parameters:
visitor
- Determines if the meets the query criteria.
Returns MEET_AND_CONTINUE to signal that the edge meets the query criteria and the query should continue.
Returns MEET_AND_STOP to signal that the edge meest the query criteria and the query should stop.
FAIL_QUERY to signal that the edge does NOT meet the query criteria.- Returns:
- A collection of edges that meet the query criteria.
- See Also:
Graph.queryEdges(GraphVisitor)
-
visitNodes
public void visitNodes(GraphVisitor visitor)
Description copied from interface:Graph
Applies the visitor pattern to the nodes of the graph.- Specified by:
visitNodes
in interfaceGraph
- See Also:
Graph.visitNodes(GraphVisitor)
-
visitEdges
public void visitEdges(GraphVisitor visitor)
Description copied from interface:Graph
Applies the visitor pattern to the edges of the graph.- Specified by:
visitEdges
in interfaceGraph
- See Also:
Graph.visitEdges(GraphVisitor)
-
getNodesOfDegree
public List<Node> getNodesOfDegree(int n)
Description copied from interface:Graph
Returns all the nodes in the graph of a specified degree. The degree of a node is the number of edges that are adjacent to the node.- Specified by:
getNodesOfDegree
in interfaceGraph
- Parameters:
n
- The desired degree of nodes to be returned.- Returns:
- A collection of nodes of degree n.
- See Also:
Graph.getNodesOfDegree(int)
,Node.getDegree()
-
getVisitedNodes
public List<Node> getVisitedNodes(boolean visited)
Description copied from interface:Graph
Returns all the nodes in the graph that have been marked as visited or non-visited.- Specified by:
getVisitedNodes
in interfaceGraph
- Parameters:
visited
- True if node is visited, false if node is unvisited.- Returns:
- List of nodes marked as visited / non-visited.
- See Also:
Graph.getVisitedNodes(boolean)
-
getVisitedEdges
public List<Edge> getVisitedEdges(boolean visited)
Description copied from interface:Graph
Returns all the edges in the graph that have been marked as visited or non-visited.- Specified by:
getVisitedEdges
in interfaceGraph
- Parameters:
visited
- True if edge is visited, false if edge is unvisited.- Returns:
- List of edges marked as visited / non-visited.
- See Also:
Graph.getVisitedEdges(boolean)
-
initNodes
public void initNodes()
Initializes the nodes in the graph by setting all visited flags to false and all visited counts to zero.
-
initEdges
public void initEdges()
Initializes the edges in the graph by setting all visited flags to false and all visited counts to zero.
-
-