Advertisement
MitchCulver

Untitled

Apr 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _311_Assignment4_Graph
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Graph why = new Graph("A");
  14.             why.BuildTestGraph();
  15.             Console.Read();
  16.         }
  17.  
  18.         public class Graph
  19.         {
  20.             Vertex head;
  21.                 // I struggled here figuring out if graphs should contain lists of
  22.                 // all its vertices and edges, so I went with an approach I was familiar with
  23.                 // from working with linked lists
  24.             public Graph(string dataOfStartVertex)
  25.             {
  26.                 head = new Vertex(dataOfStartVertex);
  27.             }
  28.             public class Edge
  29.             {
  30.                 public Vertex linkOne;
  31.                 public Vertex linkTwo;
  32.                 public int label;
  33.                     // 0 = unexplored
  34.                     // 1 = discovery edge
  35.                     // 2 = back edge
  36.  
  37.                 public Edge(Vertex origin, string newVertexData)
  38.                 {
  39.                     linkOne = origin;
  40.                     linkTwo = new Vertex(newVertexData);
  41.                     //linkTwo.connectedEdges.Add(this); // Probably should have done it this way
  42.                     label = 0;
  43.                 }
  44.                 public Edge(Vertex origin, Vertex destination)
  45.                 {
  46.                     linkOne = origin;
  47.                     linkTwo = destination;
  48.                 }
  49.             }
  50.             public class Vertex
  51.             {
  52.                 public List<Edge> connectedEdges;
  53.                 public string storedData;
  54.                 public int label;
  55.                     // 0 = unexplored
  56.                     // 1 = visted
  57.  
  58.                 public Vertex(string data)
  59.                 {
  60.                     storedData = data;
  61.                     connectedEdges = new List<Edge>();
  62.                 }                
  63.             }
  64.             public void PrintVertexInformation(Vertex vertex)
  65.             {
  66.                 if (vertex.storedData != "Head")
  67.                 {
  68.                     Console.WriteLine("> Now visiting node: " + vertex.storedData);
  69.                     Console.WriteLine("-> Exploring edges and listing them...");
  70.                     int edgeCount = 1;
  71.                     foreach (Edge edge in vertex.connectedEdges)
  72.                     {
  73.                         if (edge.label != 1)
  74.                         {
  75.                             if (edge.linkTwo.label == 1 && edge.linkOne.label == 1)
  76.                             {
  77.                                 edge.label = 2;
  78.                             }
  79.                         }
  80.                         switch (edge.label)
  81.                         {
  82.                             case 0:
  83.                                 Console.WriteLine("--> #" + edgeCount + " is an UNEXPLORED edge linking vertices " + edge.linkOne.storedData + "-" + edge.linkTwo.storedData);
  84.                                 //Console.WriteLine("---> Links vertices " + edge.linkOne.storedData + "-" + edge.linkTwo.storedData);
  85.                                 break;
  86.                             case 1:
  87.                                 Console.WriteLine("--> #" + edgeCount + " is a DISCOVERY edge linking vertices " + edge.linkOne.storedData + "-" + edge.linkTwo.storedData);
  88.                                 //Console.WriteLine("---> Links vertices " + edge.linkOne.storedData + "-" + edge.linkTwo.storedData);
  89.                                 break;
  90.                             case 2:
  91.                                 Console.WriteLine("--> #" + edgeCount + " is a BACK edge linking vertices " + edge.linkOne.storedData + "-" + edge.linkTwo.storedData);
  92.                                 //Console.WriteLine("---> Links vertices " + edge.linkOne.storedData + "-" + edge.linkTwo.storedData);
  93.                                 break;
  94.                         }
  95.                         edgeCount++;
  96.                     }
  97.                     Console.WriteLine();
  98.                 }
  99.             }
  100.             public void BuildTestGraph()
  101.                 // Lazy method to build the graph from the DFS powerpoint
  102.                 // I struggled with figuring out a good way to build a user-defined graph
  103.                 // After I realized that wasn't necessary for the assignment, I chose this method
  104.                 // I also had a problem deciding if I should have a head node pointing to A (or whatever starting node)
  105.             {
  106.                 head.connectedEdges.Add(new Edge(head, "B"));
  107.                 head.connectedEdges[0].linkTwo.connectedEdges.Add(head.connectedEdges[0]);
  108.                 // Adds the edge linking A-B to B's list
  109.                 head.connectedEdges.Add(new Edge(head, "C"));
  110.                 head.connectedEdges[1].linkTwo.connectedEdges.Add(head.connectedEdges[1]);
  111.                 // Adds the edge linking A-C to C's list
  112.                 head.connectedEdges[0].linkTwo.connectedEdges.Add
  113.                     (new Edge(head.connectedEdges[0].linkTwo, head.connectedEdges[1].linkTwo));
  114.                 // Creates the edge linking B-C, adds it to B's list
  115.                 head.connectedEdges[1].linkTwo.connectedEdges.Add(head.connectedEdges[0].linkTwo.connectedEdges[1]);
  116.                 // Adds the edge linking B-C to C's list
  117.                 head.connectedEdges.Add(new Edge(head, "D"));
  118.                 head.connectedEdges[2].linkTwo.connectedEdges.Add(head.connectedEdges[2]);
  119.                 // Adds the edge linking A-D to D's list
  120.                 head.connectedEdges[2].linkTwo.connectedEdges.Add
  121.                     (new Edge(head.connectedEdges[2].linkTwo, head.connectedEdges[1].linkTwo));
  122.                 // Creates the edge linking C-D, adds it to D's list
  123.                 head.connectedEdges[1].linkTwo.connectedEdges.Add(head.connectedEdges[2].linkTwo.connectedEdges[1]);
  124.                 // Adds the edge linking C-D to C's list
  125.                 head.connectedEdges.Add(new Edge(head, "E"));
  126.                 head.connectedEdges[3].linkTwo.connectedEdges.Add(head.connectedEdges[3]);
  127.                 // Adds the edge linking A-E to E's list
  128.                 head.connectedEdges[3].linkTwo.connectedEdges.Add
  129.                     (new Edge(head.connectedEdges[3].linkTwo, head.connectedEdges[1].linkTwo));
  130.                 // Creates the edge linking C-E, adds it to E's list
  131.                 head.connectedEdges[1].linkTwo.connectedEdges.Add(head.connectedEdges[3].linkTwo.connectedEdges[1]);
  132.                     // Adds the edge linking C-E to C's list
  133.  
  134.                 DepthFirstSearch(this, head);
  135.             }
  136.             public void DepthFirstSearch(Graph graph, Vertex vertex)
  137.             {
  138.                 vertex.label = 1; // Marks vertex as explored
  139.                 PrintVertexInformation(vertex); // Displays information about vertex                              
  140.                 foreach (Edge edge in vertex.connectedEdges)
  141.                 {                                      
  142.                     if (edge.label == 0) // If the edge is unexplored...
  143.                     {
  144.                         Vertex next = edge.linkTwo;
  145.                         if (next.label == 0) // Check the vertex it points to
  146.                         {
  147.                             // If it is also unexplored...
  148.                             edge.label = 1; // Set to discovery edge
  149.                             DepthFirstSearch(graph, next); // Then dfs it
  150.                         }
  151.                         else // If the vertex is already explored then its a back edge
  152.                         {
  153.                             edge.label = 2;                            
  154.                         }
  155.                     }                                      
  156.                 }
  157.             }
  158.             //public void BuildTestGraphWithUserInput()
  159.             //{
  160.             //    Graph graph = new Graph();
  161.             //    head.connectedEdges.Add(new Edge(head, "A"));
  162.             //    DoStuff(graph, head.connectedEdges[0].linkTwo);
  163.             //}
  164.             //public void DoStuff(Graph graph, Vertex vertex)
  165.             //{
  166.             //    PrintVertexEdges(vertex);
  167.             //    int choice = RequestGraphInput();
  168.             //}
  169.             //public void RequestGraphInput()
  170.             //{
  171.  
  172.             //}
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement