I wish did fix the issue. There are many different way to represent vertices, edges and a graph. Here is an oversimplified one: Define a directional Edge : class Edge {
private Vertex to;
private int weight;
public Edge(Vertex to, int weight) {
super();
this.to = to;
this.weight = weight;
}
Vertex getTo() {
return to;
}
int getWeight() {
return weight;
}
//todo override hashCode()
}
class Vertex {
private String label;
private Set<Edge> edges; //collection of edges to neighbors
public Vertex(String pageObject) {
this.label = pageObject;
edges = new HashSet<>();
}
String getLabel() {
return label;
}
boolean addEdge(Edge edge){
return edges.add(edge);
}
List<Edge> getEdges() {
return new ArrayList<>(edges);
}
//todo override hashCode()
}
class Graph{
private Set<Vertex> vertices; //collection of all verices
public Graph() {
vertices = new HashSet<>();
}
List<Vertex> getVertices() {
return new ArrayList<>(vertices);
}
boolean addVertex(Vertex vertex){
return vertices.add(vertex);
}
}
public static void main(String[] args) {
Graph graph = new Graph();
//construct vertices
Vertex v1 = new Vertex("1");
Vertex v2 = new Vertex("2");
Vertex v3 = new Vertex("3");
Vertex v4 = new Vertex("4");
Vertex v5 = new Vertex("5");
//to make the graph un directed use the same weight
//both ways
v1.addEdge(new Edge(v2, 1)); //connect v1 v2
v2.addEdge(new Edge(v1, 1));
v2.addEdge(new Edge(v3, 2)); //connect v2 v3
v3.addEdge(new Edge(v2, 2));
v2.addEdge(new Edge(v4, 3)); //connect v2 v4
v4.addEdge(new Edge(v2, 3));
v4.addEdge(new Edge(v5, 1)); //connect v4 v5
v5.addEdge(new Edge(v4, 1));
graph.addVertex(v1); graph.addVertex(v2); graph.addVertex(v3);
graph.addVertex(v4); graph.addVertex(v5);
}
Boards Message : 
You Must Login
Or Sign Up
to Add Your Comments . 
Share :

What are some ways I can represent a weighted, directed graph in Java?
Date : March 29 2020, 07:55 AM
To fix the issue you can do The answer depends a lot on the algorithms that you are planning to apply to your graphs. There are two common ways to represent a graph  an adjacency list and an adjacency matrix. In your case, and adjacency matrix is a square array of integers representing weights. Your representation uses an adjacency list.

How to represent given adjacency matrix as undirected weighted graph in matlab?
Date : March 29 2020, 07:55 AM
Hope this helps To get only one edge between each vertex, you only want a triangular matrix. If you try something like this: table1 = (table2 + table2')  triu((table2 + table2'))
table1 =
0 0 0 0 0
4 0 0 0 0
5 6 0 0 0
2 3 4 0 0
8 6 7 5 0
bg = biograph(table1,[],'ShowArrows','off','ShowWeights','on');
h = view(bg);

Floyd Warshall (allpairs shortest paths) on weighted undirected graph  Boost Graph
Tag : cpp , By : gcomstock
Date : March 29 2020, 07:55 AM
wish of those help I figured (with the help of the interwebs) that I need to use boost::get(boost::edge_weight, g) to directly supply an undirected weighted graph to floyd warshall. The following code compiles and works (here is a figure of the graph for the example below) #include <iostream>
#include <boost/graph/undirected_graph.hpp>
#include <boost/graph/exterior_property.hpp>
#include <boost/graph/floyd_warshall_shortest.hpp>
// type for weight/distance on each edge
typedef double t_weight;
// define the graph type
typedef boost::property<boost::edge_weight_t, t_weight> EdgeWeightProperty;
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
boost::no_property, EdgeWeightProperty> Graph;
typedef boost::property_map<Graph, boost::edge_weight_t>::type WeightMap;
// Declare a matrix type and its corresponding property map that
// will contain the distances between each pair of vertices.
typedef boost::exterior_vertex_property<Graph, t_weight> DistanceProperty;
typedef DistanceProperty::matrix_type DistanceMatrix;
typedef DistanceProperty::matrix_map_type DistanceMatrixMap;
int main()
{
Graph g;
const int num_edges = 11;
// define edges
int edges[] = { 1, 2,
1, 3,
1, 4,
2, 5,
3, 4,
3, 6,
4, 5,
4, 6,
4, 7,
5, 7,
6, 7 };
// define the weight on edges
t_weight weight[] = { 4,
10,
3,
1,
12,
20,
6,
3,
0,
3,
9 };
// iterate over all edges and insert them in the graph
for (std::size_t k = 0; k < num_edges; ++k)
boost::add_edge(edges[k*2]1, edges[k*2+1]1, weight[k], g);
WeightMap weight_pmap = boost::get(boost::edge_weight, g);
// set the distance matrix to receive the floyd warshall output
DistanceMatrix distances(num_vertices(g));
DistanceMatrixMap dm(distances, g);
// find all pairs shortest paths
bool valid = floyd_warshall_all_pairs_shortest_paths(g, dm,
boost::weight_map(weight_pmap));
// check if there no negative cycles
if (!valid) {
std::cerr << "Error  Negative cycle in matrix" << std::endl;
return 1;
}
// print upper triangular part of the distance matrix
std::cout << "Distance matrix: " << std::endl;
for (std::size_t i = 0; i < num_vertices(g); ++i) {
for (std::size_t j = i; j < num_vertices(g); ++j) {
std::cout << "From vertex " << i+1 << " to " << j+1 << " : ";
if(distances[i][j] == std::numeric_limits<t_weight>::max())
std::cout << "inf" << std::endl;
else
std::cout << distances[i][j] << std::endl;
}
std::cout << std::endl;
}
return 0;
}

How to write a toString method for a weighted undirected graph in java?
Tag : java , By : Gilmar Souza Jr.
Date : March 29 2020, 07:55 AM
I wish this help you Your definition of the method V() is recursive and probably is going into an infinite loop. You probably want it to be: public int V(){
return V;
}

What way I can represent a weighted, directed graph in Java?
Tag : java , By : Topher Cyll
Date : March 29 2020, 07:55 AM
I hope this helps . I need a method that would traverse the graph by operating on adjacency, returning the total weight of path. I'm not sure how to go about adding weight in the method "public double getPathWeight(List path)". Also, possibly I have the feeling that my public void addEdge method might contain some errors, so if I could get some pointers on that method as well, it would help me out tremendously completing my graph. My graph is written in terms of a generic VertexType parameter. And I am operating on an adjacency list data structure. , To keep it simple, I'd like you to ponder upon somethings: public void addEdge(VertexType v1, VertexType v2, double weight) {
adjacency.computeIfAbsent(v1, v > new HashMap<>()).put(v2,weight);
}
public double getPathWeight(List<VertexType> path) throws GraphPathException {
VertexType previousVertex = path.get(0);
double resultWeight = 0.0;
for (int i = 1; i < path.size(); i++) {
VertexType currentVertex = path.get(i);
Map<VertexType, Double> adjacencyForPreviousVertex = adjacency.get(previousVertex);
if (adjacencyForPreviousVertex == null) {
throw new GraphPathException("Vertex " + previousVertex + " don't exist in graph");
}
Double currentEdgeWeight = adjacencyForPreviousVertex.get(currentVertex);
if (currentEdgeWeight == null) {
throw new GraphPathException(currentVertex + "Vertex don't exist as an adjacent Vertex of " + previousVertex);
}
resultWeight += currentEdgeWeight;
previousVertex = currentVertex;
}
return resultWeight;
}

