Package ca.uqac.lif.cep.graphviz
Class Graph
java.lang.Object
ca.uqac.lif.cep.graphviz.Graph
Simple representation of a directed graph.
- Author:
- Sylvain Hallé
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Map<Integer, Set<Graph.Edge>> A collection of directed edges in this graph.protected booleanWhether to increment the weights or to replace the weight of an edge with a new value.A map associating vertices to the sum of the weights on its incoming edges.protected intA counter for giving unique numbers to vertices.A map associating labels to vertex IDs.A map associating vertex IDs to labels. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(int source, int destination, float weight) add(Graph.Edge e) Adds an edge to the graphvoidclear()Clears the contents of the graph.Creates a deep copy of the graph.duplicate(boolean with_state) Creates a deep copy of the graph.protected static StringformatWeight(float w) Removes decimals if number is an integergetEdge(int source, int destination) Finds the edge with given source and destination verticesfloatgetInWeight(int vertex) Returns the input weight of a vertexfloatgetInWeight(String vertex) Returns the input weight of a vertex by designating a vertex label.floatgetOutWeight(int vertex) Returns the output weight of a vertexincrementWeight(boolean b) Sets whether to increment the weights or to replace the weight of an edge with a new valueincrementWeight(int source, int destination, Number weight_increment) Increments the weight of an edge in the graph.incrementWeight(String source, String destination, Number weight_increment) Increments the weight of an edge in the graph, by designating it with the labels of the source and destination vertices.Renders an edge.renderVertex(String vertex) Renders a vertex.toDot()Outputs the graph in DOT format
-
Field Details
-
m_edges
A collection of directed edges in this graph. Edges are indexed by source node to facilitate lookup. -
m_vertexLabels
A map associating vertex IDs to labels. -
m_vertexIds
A map associating labels to vertex IDs. -
m_inWeights
A map associating vertices to the sum of the weights on its incoming edges. Since edges are stored by source vertex, computing the input degree would require looping through every source vertex to look for a destination vertex, which would be very costly. The alternative is to update a separate map of the input degrees as the graph is manipulated. -
m_vertexCounter
protected int m_vertexCounterA counter for giving unique numbers to vertices. -
m_incrementWeights
protected boolean m_incrementWeightsWhether to increment the weights or to replace the weight of an edge with a new value.
-
-
Constructor Details
-
Graph
public Graph()Creates a new empty graph -
Graph
Creates a new graph by copying another graph.- Parameters:
g- The graph to copy
-
-
Method Details
-
incrementWeight
Sets whether to increment the weights or to replace the weight of an edge with a new value- Parameters:
b- Set totrueto increment weights,falseto replace- Returns:
- This graph
-
add
-
add
-
add
Adds an edge to the graph- Parameters:
e- The edge- Returns:
- This graph
-
getEdge
Finds the edge with given source and destination vertices- Parameters:
source- The source vertexdestination- The destination vertex- Returns:
- The edge, or
nullif no edge could be found
-
getInWeight
Returns the input weight of a vertex by designating a vertex label.- Parameters:
vertex- The vertex label- Returns:
- The input weight, or -1 if the vertex does not exist
-
getInWeight
public float getInWeight(int vertex) Returns the input weight of a vertex- Parameters:
vertex- The vertex- Returns:
- The input weight, or -1 if the vertex does not exist
-
getOutWeight
public float getOutWeight(int vertex) Returns the output weight of a vertex- Parameters:
vertex- The vertex- Returns:
- The output weight, or 0 if the vertex does not exist
-
incrementWeight
Increments the weight of an edge in the graph, by designating it with the labels of the source and destination vertices. If the edge does not exist, a new edge is created and its weight is set to weight_increment. Note that this method assumes that each vertex in the graph has a different label in order to be used.- Parameters:
source- The label of the source vertexdestination- The label of the destination vertexweight_increment- The amount to add to the edge's weight- Returns:
- This graph
-
incrementWeight
Increments the weight of an edge in the graph. If the edge does not exist, a new edge is created and its weight is set to weight_increment.- Parameters:
source- The source vertexdestination- The destination vertexweight_increment- The amount to add to the edge's weight- Returns:
- This graph
-
toDot
Outputs the graph in DOT format- Returns:
- A DOT string
-
renderEdge
Renders an edge. Override this method to customize the rendering.- Parameters:
e- The edge to render- Returns:
- The label
-
renderVertex
Renders a vertex. Override this method to customize the rendering.- Parameters:
vertex- The string corresponding to the vertex to render- Returns:
- The Graphviz string that goes between the brackets
-
formatWeight
Removes decimals if number is an integer- Parameters:
w- The number- Returns:
- A formatted string for that number
-
duplicate
Creates a deep copy of the graph.- Returns:
- A copy of the graph
-
duplicate
Creates a deep copy of the graph.- Parameters:
with_state- Has no effect- Returns:
- A copy of the graph
-
clear
public void clear()Clears the contents of the graph.
-