Check if graph is connected
up vote
-1
down vote
favorite
I need to make a program where I got an input file then make a graph of it and check if it's connected.
For example (the input file):
R - root
L - leafs (can be G - green or B - brown)
N - nuts (R - ripe or U - unripe)
1;R;2;3
2;R;4
3;L;G
4;N;U
And the output file should be:
if the tree is connected
S: Connected
L;Z;1
L;H;0
O;Z;0
O;N;1
if the tree is not connected
S: Not Connected
This is what I got so far
public class Main {
public static void main(String args) throws IOException {
FileReader fileReader = null;
try {
fileReader = new FileReader("input.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
String strLine = bufferedReader.readLine();
// Repeat until there's no more lines to read
while (strLine != null) {
String values = strLine.split(";");
Tree treeId = new Tree(values[0]);
Tree next = new Tree(values[1]);
Tree.class
public class Tree {
String data = null;
public Tree(String data) {
this.data = data;
}
Please help me.
java graph-theory
add a comment |
up vote
-1
down vote
favorite
I need to make a program where I got an input file then make a graph of it and check if it's connected.
For example (the input file):
R - root
L - leafs (can be G - green or B - brown)
N - nuts (R - ripe or U - unripe)
1;R;2;3
2;R;4
3;L;G
4;N;U
And the output file should be:
if the tree is connected
S: Connected
L;Z;1
L;H;0
O;Z;0
O;N;1
if the tree is not connected
S: Not Connected
This is what I got so far
public class Main {
public static void main(String args) throws IOException {
FileReader fileReader = null;
try {
fileReader = new FileReader("input.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
String strLine = bufferedReader.readLine();
// Repeat until there's no more lines to read
while (strLine != null) {
String values = strLine.split(";");
Tree treeId = new Tree(values[0]);
Tree next = new Tree(values[1]);
Tree.class
public class Tree {
String data = null;
public Tree(String data) {
this.data = data;
}
Please help me.
java graph-theory
The above code may not be related to the algorithm as such. Can you post the core algorithm you tried?
– Sid
yesterday
@Sid I don't have it, that's why I need your help
– TheDumbest
yesterday
There are graph libraries for Java that implement algorithms like connectivity checks ... use JGraphT and map you data to a Graph - then use the corresponding algorithm. Those algorithms have been tested
– AKSW
yesterday
I understand. The hope at StackOverflow is to help people find issues in their programs rather than provide complete solutions to tasks. As AKSW mentioned, it may help to refer to libraries.
– Sid
yesterday
Just run a simple DFS/BFS from a single node and check if all nodes are visited. If so, graph is connected.
– Maras
yesterday
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I need to make a program where I got an input file then make a graph of it and check if it's connected.
For example (the input file):
R - root
L - leafs (can be G - green or B - brown)
N - nuts (R - ripe or U - unripe)
1;R;2;3
2;R;4
3;L;G
4;N;U
And the output file should be:
if the tree is connected
S: Connected
L;Z;1
L;H;0
O;Z;0
O;N;1
if the tree is not connected
S: Not Connected
This is what I got so far
public class Main {
public static void main(String args) throws IOException {
FileReader fileReader = null;
try {
fileReader = new FileReader("input.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
String strLine = bufferedReader.readLine();
// Repeat until there's no more lines to read
while (strLine != null) {
String values = strLine.split(";");
Tree treeId = new Tree(values[0]);
Tree next = new Tree(values[1]);
Tree.class
public class Tree {
String data = null;
public Tree(String data) {
this.data = data;
}
Please help me.
java graph-theory
I need to make a program where I got an input file then make a graph of it and check if it's connected.
For example (the input file):
R - root
L - leafs (can be G - green or B - brown)
N - nuts (R - ripe or U - unripe)
1;R;2;3
2;R;4
3;L;G
4;N;U
And the output file should be:
if the tree is connected
S: Connected
L;Z;1
L;H;0
O;Z;0
O;N;1
if the tree is not connected
S: Not Connected
This is what I got so far
public class Main {
public static void main(String args) throws IOException {
FileReader fileReader = null;
try {
fileReader = new FileReader("input.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(fileReader);
String strLine = bufferedReader.readLine();
// Repeat until there's no more lines to read
while (strLine != null) {
String values = strLine.split(";");
Tree treeId = new Tree(values[0]);
Tree next = new Tree(values[1]);
Tree.class
public class Tree {
String data = null;
public Tree(String data) {
this.data = data;
}
Please help me.
java graph-theory
java graph-theory
edited yesterday
Dominique Fortin
1,433816
1,433816
asked yesterday
TheDumbest
13
13
The above code may not be related to the algorithm as such. Can you post the core algorithm you tried?
– Sid
yesterday
@Sid I don't have it, that's why I need your help
– TheDumbest
yesterday
There are graph libraries for Java that implement algorithms like connectivity checks ... use JGraphT and map you data to a Graph - then use the corresponding algorithm. Those algorithms have been tested
– AKSW
yesterday
I understand. The hope at StackOverflow is to help people find issues in their programs rather than provide complete solutions to tasks. As AKSW mentioned, it may help to refer to libraries.
– Sid
yesterday
Just run a simple DFS/BFS from a single node and check if all nodes are visited. If so, graph is connected.
– Maras
yesterday
add a comment |
The above code may not be related to the algorithm as such. Can you post the core algorithm you tried?
– Sid
yesterday
@Sid I don't have it, that's why I need your help
– TheDumbest
yesterday
There are graph libraries for Java that implement algorithms like connectivity checks ... use JGraphT and map you data to a Graph - then use the corresponding algorithm. Those algorithms have been tested
– AKSW
yesterday
I understand. The hope at StackOverflow is to help people find issues in their programs rather than provide complete solutions to tasks. As AKSW mentioned, it may help to refer to libraries.
– Sid
yesterday
Just run a simple DFS/BFS from a single node and check if all nodes are visited. If so, graph is connected.
– Maras
yesterday
The above code may not be related to the algorithm as such. Can you post the core algorithm you tried?
– Sid
yesterday
The above code may not be related to the algorithm as such. Can you post the core algorithm you tried?
– Sid
yesterday
@Sid I don't have it, that's why I need your help
– TheDumbest
yesterday
@Sid I don't have it, that's why I need your help
– TheDumbest
yesterday
There are graph libraries for Java that implement algorithms like connectivity checks ... use JGraphT and map you data to a Graph - then use the corresponding algorithm. Those algorithms have been tested
– AKSW
yesterday
There are graph libraries for Java that implement algorithms like connectivity checks ... use JGraphT and map you data to a Graph - then use the corresponding algorithm. Those algorithms have been tested
– AKSW
yesterday
I understand. The hope at StackOverflow is to help people find issues in their programs rather than provide complete solutions to tasks. As AKSW mentioned, it may help to refer to libraries.
– Sid
yesterday
I understand. The hope at StackOverflow is to help people find issues in their programs rather than provide complete solutions to tasks. As AKSW mentioned, it may help to refer to libraries.
– Sid
yesterday
Just run a simple DFS/BFS from a single node and check if all nodes are visited. If so, graph is connected.
– Maras
yesterday
Just run a simple DFS/BFS from a single node and check if all nodes are visited. If so, graph is connected.
– Maras
yesterday
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53237877%2fcheck-if-graph-is-connected%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
The above code may not be related to the algorithm as such. Can you post the core algorithm you tried?
– Sid
yesterday
@Sid I don't have it, that's why I need your help
– TheDumbest
yesterday
There are graph libraries for Java that implement algorithms like connectivity checks ... use JGraphT and map you data to a Graph - then use the corresponding algorithm. Those algorithms have been tested
– AKSW
yesterday
I understand. The hope at StackOverflow is to help people find issues in their programs rather than provide complete solutions to tasks. As AKSW mentioned, it may help to refer to libraries.
– Sid
yesterday
Just run a simple DFS/BFS from a single node and check if all nodes are visited. If so, graph is connected.
– Maras
yesterday