-1

So, I have a winforms application with travelling salesman problem using Prim's algorithm and I need to convert my adjacency matrix, that is realized as DataGridView, to graph. Is it possible to do this?

I tried to make visualization by placing vertexes in random places on a PictureBox and connect them with lines but it didnt work out.

neonbones
  • 3
  • 3

1 Answers1

0

Yes, it is possible to convert your adjacency matrix represented in a DataGridView to a graph for the TSP problem. One way to do this is to iterate through the cells in the DataGridView and add edges to the graph for the non-zero values.

Regarding visualization, it may be more effective to use a specialized library for graph visualization such as QuickGraph.

Here is an example of how you could use QuickGraph to show your graph:

var graph = new AdjacencyGraph<string, Edge<string>>();

graph.AddVertex("A");
graph.AddVertex("B");
graph.AddVertex("C");
graph.AddVertex("D");

graph.AddEdge(new Edge<string>("A", "B", 2));
graph.AddEdge(new Edge<string>("A", "C", 3));
graph.AddEdge(new Edge<string>("B", "C", 1));
graph.AddEdge(new Edge<string>("B", "D", 4));
graph.AddEdge(new Edge<string>("C", "D", 5));

var prim = new PrimMinimumSpanningTreeAlgorithm<string, Edge<string>>(graph);
prim.Compute();

var tour = new List<string>();
foreach (var edge in prim.TreeEdges)
{
    tour.Add(edge.Source);
    tour.Add(edge.Target);
}

var g = pictureBox1.CreateGraphics();
for (int i = 0; i < tour.Count - 1; i++)
{
    g.DrawLine(Pens.Black, x[tour[i]], y[tour[i]], x[tour[i + 1]], y[tour[i + 1]]);
}