0

I have a digraph problem where I need to mark each node and print the nodes as marked and unmarked with regarding the edges in the given input and accept or reject if they are both marked in their respective start and end state in java. I have figured out how to parse each value into its individual entity, but I cannot figure out how to traverse the graph using the designated input that has gotten parsed in the form it is in regarding parentheses. Here is an example:

input : (1,2,3,4,5),((1,2),(1,3),(2,3),(2,4),(3,2),(3,5),(4,3),(5,2)),1,5
nodes are: (1,2,3,4,5)
Edges are : (1,2),(1,3),(2,3),(2,4),(3,2),(3,5),(4,3),(5,2)
Start state: 1
End state: 5

How would one go about traversing these nodes and check the edges if they are marked or unmarked? Stack, etc?

I just don't understand how to show that the node has gone to each node and marked it given these parentheses.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Sweaver
  • 63
  • 7
  • 1
    consider that you're in Java, and that you can write a Node class, an Edge class, and you can mark nodes and edges with a `boolean traveled`. Just because your input is flat, doesn't mean your code-internals should be, too. – Mike 'Pomax' Kamermans Nov 20 '14 at 18:59
  • Thanks for the input Mike. Would it be convenient to use a type of data structure however? I just don't get how to traverse each one to show that it has been traveled though given the parentheses and input. – Sweaver Nov 20 '14 at 20:28
  • you can use a Node class with a list of neighbours, and then simply rely on recursion to travel the graph, such as a `doThings() { this.visited = true; for(Node n: neighours) { if(!n.visited) n.doThings(); }}`. You can then simply pick any node in the graph as starting point, and kick off the graph walk. – Mike 'Pomax' Kamermans Nov 20 '14 at 21:40
  • Ok, I am slowly understanding the concept behind this now. My last question however is how would I start or compare such nodes with the edges in code format? Basically, the user enter's the input then it parses it into the 4 types listed above, now how do I actually begin to extract the node(s) from the nodelist and the same with the edge list to see if the nodes have been marked or not? The concept is there, but actually kicking it off from the very very beginning is where I am stuck at. – Sweaver Nov 21 '14 at 03:29
  • Use a NodeFactory, e.g. `class NodeFactory { private ArrayList nodes = new ArrayList(); public Node createNode(String label) { Node n = new Node(label, nodes.size()); nodes.add(n); return n; } public Node getNode(int id) { return nodes.get(id); }}` and then you can build your nodes as you see them in your input, and give the Node class a method to add another node as a neighbour (because the user said that was the case). – Mike 'Pomax' Kamermans Nov 21 '14 at 03:37

0 Answers0