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

Posted 1 year ago
1515 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 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
Sort By:
Posted 1 year 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]