# creating a graph from a text file

• 11-23-2009, 05:03 AM
chinolee
creating a graph from a text file
Hi everyone. im somewhat new to java programming. i want to know how to make a graph from a text file. i know how to scan in the words. i cannot figure out how to make each word a node, create edges, and find the weight between each edge. thank you for any help!!
• 11-23-2009, 06:37 AM
sky
Quote:

Hi everyone. im somewhat new to java programming. i want to know how to make a graph from a text file. i know how to scan in the words. i cannot figure out how to make each word a node, create edges, and find the weight between each edge. thank you for any help!!
This is more a design problem rather than a Java problem. First of all, you already know how nodes are going to be represented in the text file, but how about edges? They can be in the same file, or in a different one (there is many possibilities) but in both cases an edge has to contain at least the two nodes that connects.

After that, maybe you should think about class structure. It is almost sure that you will want to have a class Node and also a class Edge. Inside the class Node tipical fields could be the tag of the node, the value (if any), etc. In the class Edge, at least you have to store both nodes that are conected by the edge and some weight.

Finally, there are also many ways to represent a graph. The most tipical ones are the adjacency matrix and the adjacency list. The second one is better as more sparse the graph is.

Think about those points and If you post more details about the way that the graph is represented in the file I can tell you more.
• 11-23-2009, 07:30 AM
MuslimCoder
I think if I would do thesame program, I would either:
* make sure the point in the file is saves as a touple, e.g. (x,y)(x1,y1),(x2,y2)....etc..
* or I can just save them as numbers next to each other like x1,y1,x2,y2...so every two is a node. I can then use a vector to store them after reading, and draw one from the previous node to the next ...until there are no more.
• 11-23-2009, 09:36 AM
manhtungtnk28@gmail.com
thank everyone