Class WeightedSparseGraph<V>

java.lang.Object
org.simplegraph.impl.WeightedSparseGraph<V>
All Implemented Interfaces:
WeightedGraph<V>

public class WeightedSparseGraph<V> extends Object implements WeightedGraph<V>
Sparse graph implementation for weighted graph.
  • Field Details

  • Constructor Details

    • WeightedSparseGraph

      public WeightedSparseGraph()
      Default constructor
    • WeightedSparseGraph

      public WeightedSparseGraph(int size)
      Create a graph with a starting size.
      Parameters:
      size - starting size
    • WeightedSparseGraph

      public WeightedSparseGraph(WeightedSparseGraph<V> graph)
  • Method Details

    • addEdge

      public boolean addEdge(V v1, V v2, Double weight)
      Description copied from interface: WeightedGraph
      Add an edge between two vertices. Add the two vertices in the graph if they don't exists
      Specified by:
      addEdge in interface WeightedGraph<V>
      Parameters:
      v1 - first vertex
      v2 - second vertex
      Returns:
      true if the graph has been modified
    • getWeight

      public Double getWeight(V v1, V v2)
      Description copied from interface: WeightedGraph
      Get the edge between two vertices
      Specified by:
      getWeight in interface WeightedGraph<V>
      Parameters:
      v1 - first vertex
      v2 - second vertex
      Returns:
      the edge between v1 and v2, if it exists, null otherwise
    • addVertex

      public boolean addVertex(V vertex)
      Add a vertex to the graph.
      Parameters:
      vertex - the vertex to add
      Returns:
      true if the the graph has been modified
    • containsVertex

      public boolean containsVertex(V vertex)
      Check the existence of a vertex in the graph.
      Parameters:
      vertex - the vertex to check
      Returns:
      true if the graph contains vertex
    • countVertices

      public int countVertices()
      Get the number of vertices in the graph.
      Returns:
      number of vertices
    • existsPath

      public boolean existsPath(V source, V destination)
      Does a path exists between source and destination.
      Parameters:
      source - source vertex
      destination - destination vertex
      Returns:
      true if a path exists
    • existsEdge

      public boolean existsEdge(V v1, V v2)
      Check the existence of an edge that goes from the first vertex to the second.
      Parameters:
      v1 - first vertex
      v2 - second vertex
      Returns:
      true if an edge from v1 to v2 exists
    • countNeighbors

      public int countNeighbors(V vertex)
      Get the number of neighbors for a vertex.
      Parameters:
      vertex - the spefied vertex
      Returns:
      number of neighbors, -1 if vertex does not exists
    • getNeighbors

      public List<V> getNeighbors(V vertex)
      Get the neighbors of a vertex
      Parameters:
      vertex - the specified vertex
      Returns:
      the list of vertices that are neighbors of vertex null if the vertex is not contained in the graph
    • getPath

      public List<V> getPath(V source, V destination)
      Get a path between a source and a destination
      Parameters:
      source - source vertex
      destination - destination vertex
      Returns:
      a list containing the vertices that compose the path, in order; an empty LinkedList if there is no path, null if the source and the destination are equals or are not contained in the graph
    • removeEdge

      public boolean removeEdge(V v1, V v2)
      Remove an edge between two vertices.
      Parameters:
      v1 - first vertex
      v2 - second vertex
      Returns:
      true if the graph has been changed
    • removeVertex

      public boolean removeVertex(V vertex)
      Remove a vertex from the graph
      Parameters:
      vertex - the vertex to remove
      Returns:
      true if the graph has been modified
    • getVertices

      public List<V> getVertices()
      Get all the vertices in the graph
      Returns:
      a list containing all the graph vertices
    • getShortestPath

      public List<V> getShortestPath(V source, V destination)
      Specified by:
      getShortestPath in interface WeightedGraph<V>
    • getMinimumDistance

      public Double getMinimumDistance(V source, V destination)
      Specified by:
      getMinimumDistance in interface WeightedGraph<V>
    • getSpanningTree

      public WeightedGraph<V> getSpanningTree()
      Specified by:
      getSpanningTree in interface WeightedGraph<V>
    • getMinimumSpanningTree

      public WeightedGraph<V> getMinimumSpanningTree()
      Specified by:
      getMinimumSpanningTree in interface WeightedGraph<V>
    • copy

      public void copy(org.simplegraph.impl.BaseSparseGraph<V,Double> graph)
    • addSingleEdge

      protected boolean addSingleEdge(V v1, V v2, Double edge)
    • getSingleEdge

      protected Double getSingleEdge(V v1, V v2)
    • _removeEdge

      protected boolean _removeEdge(V v1, V v2)
    • _countEdges

      protected int _countEdges()
    • countEdges

      public int countEdges()
      Get the number of edges in the graph.
      Returns:
      number of edges