Package graphlib.paths
Class Walk<T>
java.lang.Object
graphlib.paths.Walk<T>
Represents a walk in a graph, consisting of a sequence of nodes in the order in which they are visited.
Nodes can be visited multiple times.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal voidInserts the given node at the given position in the current walk.final voidAdds a node to the end of the walk.final voidAppends a given walk at the end of the current walk.final voidInserts the given walk after the given node in the current walk.iterator()Retrieves an iterator of all nodes contained in the walk.final intlength()Retrieves the length of the walk (number of edges).final intsize()Retrieves the size of the walk (number of nodes).Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
Walk
public Walk()Creates an empty walk. -
Walk
Creates a walk containing the given nodes in the given order.- Parameters:
nodes- nodes contained in walk
-
Walk
Create a walk by copying another walk. A deep copy is made.- Parameters:
walk- walk to copy
-
-
Method Details
-
length
public final int length()Retrieves the length of the walk (number of edges). The length is defined as the number of edges followed from one node to another. Returns zero if less than two nodes have been added to the walk.- Returns:
- length of walk
-
size
public final int size()Retrieves the size of the walk (number of nodes). The size is defined as the number of nodes contained in the walk.- Returns:
- size of the walk
-
add
Adds a node to the end of the walk.- Parameters:
node- node added to the walk- Throws:
NullPointerException- ifnodeisnull
-
append
Appends a given walk at the end of the current walk. For example, given a walkw = [a,b,c,d,e], callingw.append([x,y,z])modifies the walkwto[a,b,c,d,e,x,y,z]. The given walk is copied, due to which this operation runs in linear time in the length of the appended walk.- Parameters:
walk- walk to append
-
add
Inserts the given node at the given position in the current walk. Useful for insertions at the beginnen of the walk, as this method takes linear time in the given indexi(and is independent of the length of the list).- Parameters:
i- index at whichnodeshould be insertednode- node to insert
-
insert
Inserts the given walk after the given node in the current walk. This method can be used to join walks. For example, given a walkw = [a,b,c,d,e], callingw.insert(b, [x,y,z])modifies the walkwto[a,b,x,y,z,c,d,e]. The given walk is copied when inserting, due to which an insert runs in linear time in the length of the inserted walk.In case the given node occurs multiple times in the current walk, the given walk is inserted after an arbitrary occurrence of this node. For example, given a walk
w = [a,b,c,b,e], callingw.insert(b, [x,y,z])either modifies the walkwto[a,b,x,y,z,c,b,e]or to[a,b,c,b,x,y,z,e](which one is unpredictable).Note that, given a walk
w = [a,b,c,d,e], callingw.insert(e, [x,y,z])is equivalent to callingw.append([x,y,z])and modifies the walkwto[a,b,c,d,e,x,y,z].- Parameters:
node- node after which the walk is to be insertedwalk- walk to insert- Throws:
IllegalArgumentException- if the given node does not occur in the walk
-
iterator
Retrieves an iterator of all nodes contained in the walk. Nodes are returned by this iterator in the order in which they are visited. Possibly, the same node can be visited multiple times.
-