H
H
hana012020-12-03 21:16:17
Java
hana01, 2020-12-03 21:16:17

How to visualize graphs in Java?

Let's say I have a code in java to build an Euler cycle, I need to visualize it. I've been studying Java for a while now, I know, perhaps, only the basics. The search for the necessary means for this did not lead to anything concrete, except for advice to try using JavaFX. I know it's handy for charting, but will it work for my purposes? If not, what should be used? And what literature do you recommend?
The code looks like this:

package OZP;

import java.util.*;

public class Main {
static void dfs(List[] graph, int[] curEdge, ArrayList unusedEdges, List res, int u) {
while (curEdge[u] < graph[u].size()) {
int v = graph[ u].get(curEdge[u]++);

boolean b = false;
for (Object o : unusedEdges) {
int[] a = (int[])o;
if ( (a[0] == u && a[1] == v) || (a[0] == v && a[1] == u) ) {
b = true;
unusedEdges.remove(o);
break;
}
}

if (b) dfs(graph, curEdge, unusedEdges, res, v);
}
res.add(u);
}

public static boolean[] BFS(boolean[][] adjacencyMatrix, int vertexCount, int givenVertex){
// Result array.
boolean[] mark = new boolean[vertexCount];

Queue queue = new LinkedList();
queue.add(givenVertex);
mark[givenVertex] = true;

while (!queue.isEmpty())
{
Integer current = queue.remove();

for (int i = 0; i < vertexCount; ++i)
if (adjacencyMatrix[current][i] && !mark[i])
{
mark[i] = true;
queue.add(i);
}
}

return mark;
}

public static List eulerCycleUndirected(List[] graph, int u, ArrayList unusedEdges) {
int n = graph.length;
int[] curEdge = new int[n];
Listres = new ArrayList<>();
dfs(graph, curEdge, unusedEdges, res, u);
Collections.reverse(res);
return res;
}

public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the number of vertices and edges (separated by spaces): ");
int v = s.nextInt(), e = s.nextInt();

ArrayList unusedEdges = new ArrayList();
boolean[][] adjacencyMatrix = new boolean[v][v];
System.out.println("Enter pairs of vertices connected by an edge (separated by a space): ");

List[] g = new List[v];
for (int i = 0; i < v; i++) g[i] = new ArrayList<>();
for (int i = 0; i < e; i++) {
int a = s.nextInt() - 1, b = s.nextInt() - 1;
unusedEdges.add(new int[]{a, b});
g[a].add(b);
g[b].add(a);

adjacencyMatrix[a][b] = adjacencyMatrix[b][a] = true;
}

boolean[] mark = BFS(adjacencyMatrix, v, 0);
for (int i = 0; i < mark.length; i++) {
if (!mark[i]) {
System.out.println("NONE");
return;
}
}

List res = eulerCycleUndirected(g, 0, unusedEdges);
if (res.get(0).equals(res.get(res.size() - 1))) {
for (int j = 0; j < res.size(); j++) System.out.print(( res.get(j) + 1) + " ");
return;
}

System.out.println("NONE");
}

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mayton2019, 2020-12-03
@mayton2019

Check out the graphviz utility. It allows you to beautifully format text files with vertices and edges into pictures.
Look at the graphic editor yEd. It seems to have plugins and APIs for external development.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question