Advertisement
Matixs

Untitled

May 13th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using QuickGraph;
  6. using QuickGraph.Algorithms.Observers;
  7. using QuickGraph.Algorithms.ShortestPath;
  8. using QuickGraph.Graphviz;
  9.  
  10. namespace Lab7Exc2
  11. {
  12.     public class Node
  13.     {
  14.         public int Value { get; set; }
  15.         public Node Next { get; set; }
  16.  
  17.         public Node(int value)
  18.         {
  19.             Value = value;
  20.             Next = null;
  21.         }
  22.     }
  23.  
  24.     public class Graph
  25.     {
  26.         public int NumVertices { get; set; }
  27.         public Node[] AdjList { get; set; }
  28.         public bool[] IsIsolated { get; set; }
  29.  
  30.         public Graph(int numVertices)
  31.         {
  32.             if (numVertices < 1)
  33.             {
  34.                 throw new ArgumentException("Invalid number of vertices");
  35.             }
  36.             NumVertices = numVertices;
  37.             AdjList = new Node[numVertices + 1];
  38.             IsIsolated = new bool[numVertices + 1];
  39.         }
  40.  
  41.         public void AddEdge(int src, int dest)
  42.         {
  43.             if (src < 1 || src > NumVertices || dest < 1 || dest > NumVertices)
  44.             {
  45.                 throw new ArgumentException("Invalid vertex number");
  46.             }
  47.             var newNode = new Node(dest);
  48.             if (AdjList[src] == null)
  49.             {
  50.                 AdjList[src] = newNode;
  51.             }
  52.             else
  53.             {
  54.                 newNode.Next = AdjList[src];
  55.                 AdjList[src] = newNode;
  56.             }
  57.             IsIsolated[src] = false;
  58.             IsIsolated[dest] = false;
  59.         }
  60.  
  61.         public void FindIsolatedVertices()
  62.         {
  63.             for (int i = 1; i <= NumVertices; i++)
  64.             {
  65.                 if (AdjList[i] == null)
  66.                 {
  67.                     IsIsolated[i] = true;
  68.                 }
  69.             }
  70.         }
  71.  
  72.         public static Graph SetGraphFromConsole(bool allowIsolated)
  73.         {
  74.             try
  75.             {
  76.                 Console.Write("Enter the number of vertices: ");
  77.                 int numVertices = int.Parse(Console.ReadLine());
  78.                 var graph = new Graph(numVertices);
  79.  
  80.                 if (numVertices == 1)
  81.                 {
  82.                     Console.WriteLine("The graph contains only one vertex.");
  83.                     return graph;
  84.                 }
  85.  
  86.                 if (allowIsolated)
  87.                 {
  88.                     Console.Write("Enter the isolated vertices (space-separated, or press enter to skip): ");
  89.                     string[] isolatedVertices = Console.ReadLine().Split();
  90.                     foreach (string s in isolatedVertices)
  91.                     {
  92.                         int isolated = int.Parse(s);
  93.                         graph.IsIsolated[isolated] = true;
  94.                     }
  95.                 }
  96.  
  97.                 Console.WriteLine("Enter the adjacency list for each vertex:");
  98.                 for (int i = 1; i <= numVertices; i++)
  99.                 {
  100.                     Console.Write(i + ": ");
  101.                     string[] adjList = Console.ReadLine().Split();
  102.                     foreach (string s in adjList)
  103.                     {
  104.                         int dest = int.Parse(s);
  105.                         graph.AddEdge(i, dest);
  106.                     }
  107.                 }
  108.                 return graph;
  109.             }
  110.             catch (Exception e)
  111.             {
  112.                 Console.WriteLine("Error: " + e.Message);
  113.                 return null;
  114.             }
  115.         }
  116.  
  117.         public static Graph SetGraphFromFile(string fileName)
  118.         {
  119.             try
  120.             {
  121.                 if (!File.Exists(fileName))
  122.                 {
  123.                     Console.WriteLine("File not found");
  124.                     return null;
  125.                 }
  126.                 using (StreamReader sr = new StreamReader(fileName))
  127.                 {
  128.                     int numVertices = int.Parse(sr.ReadLine());
  129.                     var graph = new Graph(numVertices);
  130.  
  131.                     if (numVertices == 1)
  132.                     {
  133.                         Console.WriteLine("The graph contains only one vertex.");
  134.                         return graph;
  135.                     }
  136.  
  137.                     string[] isolatedVertices = sr.ReadLine().Split();
  138.                     foreach (string s in isolatedVertices)
  139.                     {
  140.                         int isolated = int.Parse(s);
  141.                         graph.IsIsolated[isolated] = true;
  142.                     }
  143.  
  144.                     for (int i = 1; i <= numVertices; i++)
  145.                     {
  146.                         string[] adjList = sr.ReadLine().Split();
  147.                         foreach (string s in adjList)
  148.                         {
  149.                             int dest = int.Parse(s);
  150.                             graph.AddEdge(i, dest);
  151.                         }
  152.                     }
  153.                     return graph;
  154.                 }
  155.             }
  156.             catch (Exception e)
  157.             {
  158.                 Console.WriteLine("Error: " + e.Message);
  159.                 return null;
  160.             }
  161.         }
  162.         public void VisualizeVertexCover(int[] cover)
  163.         {
  164.             Console.WriteLine("Graph:");
  165.             for (int i = 1; i <= NumVertices; i++)
  166.             {
  167.                 Console.Write(i + ": ");
  168.                 var current = AdjList[i];
  169.                 while (current != null)
  170.                 {
  171.                     Console.Write(current.Value + " ");
  172.                     current = current.Next;
  173.                 }
  174.                 Console.WriteLine();
  175.             }
  176.             Console.WriteLine("Vertex cover:");
  177.             for (int i = 1; i <= NumVertices; i++)
  178.             {
  179.                 if (cover.Contains(i))
  180.                 {
  181.                     Console.ForegroundColor = ConsoleColor.Green;
  182.                 }
  183.                 Console.Write(i + " ");
  184.                 Console.ResetColor();
  185.             }
  186.             Console.WriteLine();
  187.         }
  188.  
  189.         public int[] FindVertexCover()
  190.         {
  191.             FindIsolatedVertices();
  192.             bool[] visited = new bool[NumVertices + 1];
  193.             int[] cover = new int[NumVertices];
  194.             int coverSize = 0;
  195.             for (int i = 1; i <= NumVertices; i++)
  196.             {
  197.                 if (!visited[i] && !IsIsolated[i])
  198.                 {
  199.                     visited[i] = true;
  200.                     cover[coverSize++] = i;
  201.                     var current = AdjList[i];
  202.                     while (current != null)
  203.                     {
  204.                         visited[current.Value] = true;
  205.                         current = current.Next;
  206.                     }
  207.                 }
  208.             }
  209.             Array.Resize(ref cover, coverSize);
  210.             return cover;
  211.         }
  212.     }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement