Advertisement
Guest User

C# solution

a guest
Apr 6th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Collections;
  11.  
  12. namespace SDA_Pr09_02
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         public Form1()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.  
  21.         Graph g = new Graph();
  22.         private void btnCreateGraph1_Click(object sender, EventArgs e)
  23.         {
  24.             g.ClearGraph();
  25.             g.AddVertex("A"); g.AddVertex("B");
  26.             g.AddVertex("C"); g.AddVertex("D");
  27.             g.AddVertex("E");
  28.             g.AddEdge(0, 1, 5); g.AddEdge(1, 0, 5);
  29.             g.AddEdge(0, 2, 2); g.AddEdge(2, 0, 2);
  30.             g.AddEdge(1, 2, 2); g.AddEdge(2, 1, 2);
  31.             g.AddEdge(1, 4, 4); g.AddEdge(4, 1, 4);
  32.             g.AddEdge(2, 3, 8); g.AddEdge(3, 2, 8);
  33.             g.AddEdge(3, 4, 10); g.AddEdge(4, 3, 10);
  34.  
  35.             richTextBox1.Text = g.ShowMatrix();
  36.         }
  37.  
  38.         private void btnCreateGraph2_Click(object sender, EventArgs e)
  39.         {
  40.             g.ClearGraph();
  41.             g.AddVertex("A"); g.AddVertex("B");
  42.             g.AddVertex("C"); g.AddVertex("D");
  43.             g.AddVertex("E");
  44.             g.AddEdge(0, 1, 5); g.AddEdge(1, 0, 5);
  45.             g.AddEdge(0, 2, 2); g.AddEdge(2, 0, 2);
  46.             g.AddEdge(1, 2, 2); g.AddEdge(2, 1, 2);
  47.             g.AddEdge(1, 4, 4); g.AddEdge(4, 1, 4);
  48.             g.AddEdge(2, 3, 6); g.AddEdge(3, 2, 6);
  49.             g.AddEdge(3, 4, 1); g.AddEdge(4, 3, 1);
  50.  
  51.             richTextBox1.Text = g.ShowMatrix();
  52.         }
  53.  
  54.         private void btnCreateGraph3_Click(object sender, EventArgs e)
  55.         {
  56.             g.ClearGraph();
  57.             g.AddVertex("A"); g.AddVertex("B");
  58.             g.AddVertex("C"); g.AddVertex("D");
  59.             g.AddVertex("E");
  60.             g.AddEdge(0, 1, 5); g.AddEdge(1, 0, 5);
  61.             g.AddEdge(0, 2, 2); g.AddEdge(2, 0, 2);
  62.             g.AddEdge(1, 2, 2); g.AddEdge(2, 1, 2);
  63.             g.AddEdge(1, 4, 4); g.AddEdge(4, 1, 4);
  64.             g.AddEdge(2, 3, 2); g.AddEdge(3, 2, 2);
  65.             g.AddEdge(3, 4, 1); g.AddEdge(4, 3, 1);
  66.  
  67.             richTextBox1.Text = g.ShowMatrix();
  68.         }
  69.  
  70.         public class Vertex
  71.         {
  72.             public bool wasVisited;
  73.             public string label;
  74.             public Vertex(string label)
  75.             {
  76.                 this.label = label;
  77.                 wasVisited = false;
  78.             }
  79.         }
  80.  
  81.         public class DistanceFromFirstVertex
  82.         {
  83.             public int distance;
  84.             public int parentVert;
  85.             public DistanceFromFirstVertex(int pv, int d)
  86.             {
  87.                 distance = d;
  88.                 parentVert = pv;
  89.             }
  90.         }
  91.  
  92.         public class Graph
  93.         {
  94.             private const int max_verts = 20;
  95.             int infinity = Int32.MaxValue;
  96.             Vertex[] vertexList;
  97.             int[,] adjMatrix;
  98.             int nVerts;
  99.             int nTree;
  100.             string str = "";
  101.             DistanceFromFirstVertex[] shortPath;
  102.             int currentVert;
  103.             int startToCurrent;
  104.             public Graph()
  105.             {
  106.                 vertexList = new Vertex[max_verts];
  107.                 adjMatrix = new int[max_verts, max_verts];
  108.                 nVerts = 0;
  109.                 nTree = 0;
  110.                 for (int j = 0; j <= max_verts - 1; j++)
  111.                     for (int k = 0; k <= max_verts - 1; k++)
  112.                         adjMatrix[j, k] = infinity;
  113.                 shortPath = new DistanceFromFirstVertex[max_verts];
  114.             }
  115.             public void AddVertex(string lab)
  116.             {
  117.                 vertexList[nVerts] = new Vertex(lab);
  118.                 nVerts++;
  119.             }
  120.             public void AddEdge(int start, int theEnd, int weight)
  121.             {
  122.                 adjMatrix[start, theEnd] = weight;
  123.                 adjMatrix[theEnd, start] = weight;
  124.             }
  125.             public bool HasEdge(int start, int theEnd)
  126.             {
  127.                 if (adjMatrix[start, theEnd] == 0)
  128.                 {
  129.                     return false;
  130.                 }
  131.                 else
  132.                 {
  133.                     return true;
  134.                 }
  135.             }
  136.             public int GetMin()
  137.             {
  138.                 int minDist = infinity;
  139.                 int indexMin = 0;
  140.                 for (int j = 1; j <= nVerts - 1; j++)
  141.                 {
  142.                     if (!(vertexList[j].wasVisited) && shortPath[j].distance < minDist)
  143.                     {
  144.                         minDist = shortPath[j].distance;
  145.                         indexMin = j;
  146.                     }
  147.                 }
  148.                 return indexMin;
  149.             }
  150.             public void Path()
  151.             {
  152.                 int startTree = 0;
  153.                 vertexList[startTree].wasVisited = true;
  154.                 nTree = 1;
  155.                 for (int j = 0; j < nVerts; j++)
  156.                 {
  157.                     int tempDist = adjMatrix[startTree, j];
  158.                     shortPath[j] = new DistanceFromFirstVertex(startTree, tempDist);
  159.                 }
  160.                 for (int i = 0; i < nVerts; i++)
  161.                 {
  162.                     str += i + "=" + shortPath[i].distance + ", ";
  163.                 }
  164.                 while (nTree < nVerts)
  165.                 {
  166.                     int indexMin = GetMin();
  167.                     currentVert = indexMin;
  168.                     int minDist = shortPath[indexMin].distance;
  169.                     startToCurrent = shortPath[indexMin].distance;
  170.                     vertexList[currentVert].wasVisited = true;
  171.                     CalculateShortPath();
  172.                     nTree++;
  173.                 }
  174.             }
  175.             public void CalculateShortPath()
  176.             {
  177.                 int idVerts = 1;
  178.                 while (idVerts < nVerts)
  179.                 {
  180.                     if (vertexList[idVerts].wasVisited) idVerts++;
  181.                     else
  182.                     {
  183.                         int currentToFringe = adjMatrix[currentVert, idVerts];
  184.                         if (currentToFringe == infinity)
  185.                         {
  186.                             idVerts++;
  187.                             continue;
  188.                         }
  189.                         int startToFringe = startToCurrent + currentToFringe;
  190.                         int sPathDist = shortPath[idVerts].distance;
  191.                         if (startToFringe < sPathDist)
  192.                         {
  193.                             shortPath[idVerts].parentVert = currentVert;
  194.                             shortPath[idVerts].distance = startToFringe;
  195.                         }
  196.                         idVerts++;
  197.                     }
  198.                 }
  199.             }
  200.             public string DisplaySolution()
  201.             {
  202.                 string s = "\nРешение:\n";
  203.                 s += String.Format("{0,8} {1,8} {2,5} \n", " До възел", "Родител", "Път");
  204.                 for (int j = 0; j < nVerts; j++)
  205.                 {
  206.                     s += String.Format(" {0,5} ", vertexList[j].label);
  207.                     string parent = vertexList[shortPath[j].parentVert].label;
  208.                     s += String.Format(" {0,5} ", parent);
  209.                     if (shortPath[j].distance == infinity)
  210.                         s += String.Format(" {0,5} \n", "   inf");
  211.                     else
  212.                         s += String.Format(" {0,5} \n", shortPath[j].distance.ToString());
  213.                 }
  214.                 return s;
  215.             }
  216.             public string ShowVertex(int v)
  217.             {
  218.                 return (vertexList[v].label);
  219.             }
  220.             public int Count()
  221.             {
  222.                 return nVerts;
  223.             }
  224.             public void ClearGraph()
  225.             {
  226.                 for (int i = 0; i < nVerts; i++)
  227.                     vertexList[i] = null;
  228.                 for (int j = 0; j < nVerts; j++)
  229.                     for (int k = 0; k < nVerts; k++)
  230.                         adjMatrix[j, k] = infinity;
  231.                 for (int j = 0; j < nVerts; j++)
  232.                     vertexList[j].wasVisited = false;
  233.                 nVerts = 0;
  234.                 nTree = 0;
  235.             }
  236.             public string ShowMatrix()
  237.             {
  238.                 if (nVerts == 0) return "";
  239.                 string s = "                            |                      ";
  240.                 for (int i = 0; i < nVerts; i++)
  241.                 {
  242.                     s += String.Format("    {0} |                  ", ShowVertex(i));
  243.                 }
  244.                 s += "\n";
  245.  
  246.                 for (int i = 0; i <= nVerts; i++)
  247.                 {
  248.                     s += "==============|";
  249.                 }
  250.                 s += "\n";
  251.                 for (int j = 0; j < nVerts; j++)
  252.                 {
  253.                     s += String.Format("                       {0}   |", ShowVertex(j));
  254.                     for (int k = 0; k < nVerts; k++)
  255.                     {
  256.                         if (HasEdge(j, k))
  257.                         {
  258.                             s +="                 1         |";
  259.                         }
  260.                         else
  261.                         {
  262.                             s += "  - |";
  263.                         }
  264.                     }
  265.                     s += "\n";
  266.                 }
  267.                 return s;
  268.             }
  269.         }
  270.     }
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement