|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectGraph
AdjListsGraph
This is a graph implementation that uses adjacency lists to store edges. It contains one linked-list for every node i of the graph. The list for i contains one instance of AdjListElmnt for every node that is adjacent to i. In case the graph is directed, if there is an edge from node i to node j then there is a corresponding element in the adjacency lists of node i (only). In case the graph is undirected, if there is an edge between nodes i and j, then there is a corresponding element in the adjacency lists of *both* nodes i and j. The lists are not sorted; they contain the adjacent nodes in *reverse* order of edge insertion. In other words, for a directed graph, the node at the head of the list of some node i corresponds to the edge with source i that was added to the graph most recently (and has not been removed, yet). Similarly, for an undirected graph, the node at the head of the list of some node i corresponds to the edge with source or target i that was added to the graph most recently (and has not been removed, yet).
Field Summary |
Fields inherited from class Graph |
directed, nNodes |
Constructor Summary | |
AdjListsGraph(int nodes,
boolean directed)
The class constructor. |
Method Summary | |
void |
addEdge(int source,
int target,
double weight)
This method adds an edge to the graph without checking whether the edge already exists. |
boolean |
addEdgeSafe(int source,
int target,
double weight)
This method adds an edge to the graph as follows: first check whether the edge already exists in the graph; if it does not exist, then add the edge and return true; if it does exist, then do nothing and return false. |
java.util.LinkedList |
getAdjListOfNode(int i)
This method returns the adjacency linked-list of node i. |
void |
removeAllEdges()
This method removes all edges in the graph. |
double |
removeEdge(int source,
int target)
This method removes an edge as follows: if the graph contains an edge from source to target node, then this edge is removed and the weight of the edge is returned. |
Graph |
toAltGraphRepr(Graph g)
This method copies this graph to graph g, if g has the same number of nodes, and both g and this graph are directed, or both are undirected. |
java.lang.String |
toString()
|
double |
weightOfEdge(int source,
int target)
This method returns the weight of the edge from the source node to the target node, or +Infinity if no such edge exists. |
Methods inherited from class Graph |
addEdge, addEdgeSafe, getNNodes, isDirected |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public AdjListsGraph(int nodes, boolean directed)
nodes
- the number of nodes.directed
- true if the graph is be directed.Method Detail |
public void addEdge(int source, int target, double weight)
Graph
addEdge
in class Graph
source
- the source of the edge to be added to the graph.target
- the target of the edge to be added to the graph.weight
- the weight of the edge to be added to the graph.public boolean addEdgeSafe(int source, int target, double weight)
Graph
addEdgeSafe
in class Graph
source
- the source of the edge to be added to the graph.target
- the target of the edge to be added to the graph.weight
- the weight of the edge to be added to the graph.
public double removeEdge(int source, int target)
Graph
removeEdge
in class Graph
source
- the source node of the edge to remove.target
- the target node of the edge to remove.
public void removeAllEdges()
Graph
removeAllEdges
in class Graph
public double weightOfEdge(int source, int target)
Graph
weightOfEdge
in class Graph
source
- the source node of the edge.target
- the target node of the edge.
public java.lang.String toString()
public Graph toAltGraphRepr(Graph g)
Graph
toAltGraphRepr
in class Graph
g
- the graph to copy this graph to.
public java.util.LinkedList getAdjListOfNode(int i)
i
- node
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |