Class Path
- All Implemented Interfaces:
Structure
public class Path extends Object implements Structure
A path is a class that stores ordered lists of nodes and links that are adjacent. Such a path may be manipulated with nodes and/or edges added or removed. This class is designed as a dynamic structure that is, to add edges during the construction of the path. Only edges need to be added, the nodes list is maintained automatically.
The two lists (one for nodes, one for edges) may be acceded at any moment in constant time.
The constraint of this class is that it needs to know the first node of the
path (the root). This root can be set with the setRoot(Node)
method
or by using the add(Node, Edge)
method.
The normal use with this class is to first use the setRoot(Node)
method to initialize the path; then to use the add(Edge)
method to
grow it and the popEdge()
or popNode()
.
-
Constructor Summary
Constructors Constructor Description Path()
New empty path. -
Method Summary
Modifier and Type Method Description void
add(Edge edge)
Adds an edge to the path.void
add(Node from, Edge edge)
Adds a node and an edge to the path.void
clear()
Clears the path;boolean
contains(Edge edge)
Says whether the path contains this edge or not.boolean
contains(Node node)
Says whether the path contains this node or not.Stream<Edge>
edges()
boolean
empty()
Returns true if the path is empty.boolean
equals(Path p)
Compare the content of the current path and the specified path to decide weather they are equal or not.Path
getACopy()
Get a copy of this pathint
getEdgeCount()
Number of edges in this graph.List<Edge>
getEdgePath()
Returns the list of edges representing the path.<T extends Edge>
Collection<T>getEdgeSet()
int
getNodeCount()
Returns the size of the path.List<Node>
getNodePath()
Construct an return a list of nodes that represents the path.<T extends Node>
Collection<T>getNodeSet()
Double
getPathWeight(String characteristic)
It returns the sum of thecharacteristic
given value in the Edges of the path.Node
getRoot()
Get the root (the first node) of the path.Stream<Node>
nodes()
Edge
peekEdge()
Looks at the edge at the top of the stack without removing it from the stack.Node
peekNode()
Looks at the node at the top of the stack without removing it from the stack.Edge
popEdge()
This methods pops the 2 stacks (edgePath
andnodePath
) and returns the removed edge.Node
popNode()
This methods pops the 2 stacks (edgePath
andnodePath
) and returns the removed node.void
push(Edge edge)
A synonym foradd(Edge)
.void
push(Node from, Edge edge)
A synonym foradd(Edge)
.void
removeLoops()
Remove all parts of the path that start at a given node and pass a new at this node.void
setRoot(Node root)
Set the root (first node) of the path.int
size()
Returns the size of the pathString
toString()
Returns a String description of the path.
-
Constructor Details
-
Path
public Path()New empty path.
-
-
Method Details
-
getRoot
Get the root (the first node) of the path.- Returns:
- the root of the path.
-
setRoot
Set the root (first node) of the path.- Parameters:
root
- The root of the path.
-
contains
Says whether the path contains this node or not.- Parameters:
node
- The node tested for existence in the path.- Returns:
true
if the path contains the node.
-
contains
Says whether the path contains this edge or not.- Parameters:
edge
- The edge tested for existence in the path.- Returns:
true
if the path contains the edge.
-
empty
public boolean empty()Returns true if the path is empty.- Returns:
true
if the path is empty.
-
size
public int size()Returns the size of the path -
getPathWeight
It returns the sum of thecharacteristic
given value in the Edges of the path.- Parameters:
characteristic
- The characteristic.- Returns:
- Sum of the characteristics.
-
getEdgePath
Returns the list of edges representing the path.- Returns:
- The list of edges representing the path.
-
getNodePath
Construct an return a list of nodes that represents the path.- Returns:
- A list of nodes representing the path.
-
add
Adds a node and an edge to the path. If root is not set, the node will be set as root. Otherwise from node must be the same as the head node of the path.- Parameters:
from
- The start node.edge
- The edge used.
-
add
Adds an edge to the path.- Parameters:
edge
- The edge to add to the path.
-
push
A synonym foradd(Edge)
. -
push
A synonym foradd(Edge)
. -
popEdge
This methods pops the 2 stacks (edgePath
andnodePath
) and returns the removed edge.- Returns:
- The edge that have just been removed.
-
popNode
This methods pops the 2 stacks (edgePath
andnodePath
) and returns the removed node.- Returns:
- The node that have just been removed.
-
peekNode
Looks at the node at the top of the stack without removing it from the stack.- Returns:
- The node at the top of the stack.
-
peekEdge
Looks at the edge at the top of the stack without removing it from the stack.- Returns:
- The edge at the top of the stack.
-
clear
public void clear()Clears the path; -
getACopy
Get a copy of this path- Returns:
- A copy of this path.
-
removeLoops
public void removeLoops()Remove all parts of the path that start at a given node and pass a new at this node. -
equals
Compare the content of the current path and the specified path to decide weather they are equal or not.- Parameters:
p
- A path to compare to the curent one.- Returns:
- True if both paths are equal.
-
toString
Returns a String description of the path. -
getNodeCount
public int getNodeCount()Returns the size of the path. Identical tosize()
.- Specified by:
getNodeCount
in interfaceStructure
- Returns:
- The size of the path.
-
getEdgeCount
public int getEdgeCount()Description copied from interface:Structure
Number of edges in this graph.- Specified by:
getEdgeCount
in interfaceStructure
- Returns:
- The number of edges.
-
nodes
-
edges
-
getNodeSet
-
getEdgeSet
-