Message Boards Message Boards

0
|
3506 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

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

Posted 4 years ago

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
POSTED BY: huakai zeng
2 Replies
Posted 4 years 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

POSTED BY: Rohit Namjoshi
Posted 4 years ago

Thank you sir! This works great!

POSTED BY: huakai zeng
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