Message Boards Message Boards

GROUPS:

[?] Read and map data from a file and create a graph?

Posted 9 months ago
876 Views
|
2 Replies
|
2 Total Likes
|

Hi I am new to Mathematica and i'm trying to read a .txt file that has 3 elements in each line separated by a whitespace. The .txt file can have as many lines as it wants. I am trying to set up a Directed Weighted Graph using the data in the .txt file while reading it

.txt file goes like this enter image description here

E.g. ( I want to construct a directed weighted graph like this while reading the file

enter cograph = Graph[{"Adam" -> "Sally", "Adam" -> "Bob", "Sally" -> "Bob"}, EdgeWeight-> {10, 5, 3}]de here
2 Replies
Posted 9 months ago

Thank you sir! This works great!

Posted 9 months ago

Hi Huakai,

data = Import["~/file.txt", "Table"]
edges = Thread[data[[All, 1]] -> data[[All, 2]]]
weights = data[[All, 3]]
g = Graph[edges, EdgeWeight -> weights, VertexLabels -> Automatic]

enter image description here

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract