Julia
Julia

Reputation: 1399

Mathematica : duplicate edges

I am trying to generate a graph from an adjacency list, but Mathematica doesn't want to plot my graph because of multiple edges i think. This is my script :

Needs["GraphUtilities`"]
data = Import["adj_matrix.txt", "Table"];
data2 = Flatten[Table[{data[[i, 1]] \[UndirectedEdge] data[[i, 2]]}, {i, 1, 
  Length[data]}]];
graph1 = Graph[data2]

The error i get is : Graph::supp: Mixed graphs and multigraphs are not supported.

I do not have any "proper" duplicates in my list, the only type of duplicates that appear are 1->2 and 2->1. I would like to know how to delete these "duplicates" from my list.

Upvotes: 1

Views: 1267

Answers (1)

Szabolcs
Szabolcs

Reputation: 25703

You can directly convert an adjacency matrix to a Graph using AdjacencyGraph[]. This is probably the simplest solution:

AdjacencyGraph[data]

If your matrix contains other elements than 0 and 1, use

AdjacencyGraph@Clip[data]

To filter out duplicates from the edge list of an undirected graph, use

Union[Sort /@ edgeList]

Upvotes: 4

Related Questions