Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab7Exc2
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Graph g = null;
- int[] vertexCover = null;
- while (true)
- {
- Console.Clear();
- Console.WriteLine("1. Set graph from console");
- Console.WriteLine("2. Set graph from file");
- Console.WriteLine("3. Set vertex cover");
- Console.WriteLine("4. Export vertex cover to file");
- Console.WriteLine("5. Print graph");
- Console.WriteLine("6. Exit");
- Console.WriteLine("Example for import:");
- Console.WriteLine("4");
- Console.WriteLine("0 // the constant is always set");
- Console.WriteLine("1 3 2");
- Console.WriteLine("2 1 3");
- Console.WriteLine("3 1 2 4" );
- Console.WriteLine("4 3");
- Console.Write("Input: ");
- int choice;
- if (!int.TryParse(Console.ReadLine(), out choice) || choice < 1 || choice > 6)
- {
- Console.WriteLine("Invalid choice, please try again");
- Console.ReadKey();
- continue;
- }
- switch (choice)
- {
- case 1:
- g = Graph.SetGraphFromConsole(false);
- Console.ReadKey();
- break;
- case 2:
- try
- {
- Console.Write("Enter the file name: ");
- string fileName = Console.ReadLine();
- g = Graph.SetGraphFromFile(fileName);
- Console.ReadKey();
- }
- catch (Exception e)
- {
- Console.WriteLine("Error: " + e.Message);
- }
- break;
- case 3:
- try
- {
- if (g == null)
- {
- Console.WriteLine("Graph not set");
- Console.ReadKey();
- break;
- }
- vertexCover = FindVertexCover(g);
- Console.WriteLine("Vertex cover: " + string.Join(" ", vertexCover));
- Console.ReadKey();
- }
- catch (Exception e)
- {
- Console.WriteLine("Error: " + e.Message);
- }
- break;
- case 4:
- try
- {
- if (g == null)
- {
- Console.WriteLine("Graph not set");
- Console.ReadKey();
- break;
- }
- Console.Write("Enter the file name: ");
- string fileName = Console.ReadLine();
- using (StreamWriter sw = new StreamWriter(fileName))
- {
- vertexCover = FindVertexCover(g);
- sw.WriteLine(string.Join(" ", vertexCover));
- }
- Console.ReadKey();
- }
- catch (Exception e)
- {
- Console.WriteLine("Error: " + e.Message);
- }
- break;
- case 5:
- if (vertexCover == null)
- {
- Console.WriteLine("Vertex cover not set");
- Console.ReadKey();
- break;
- }
- g.VisualizeVertexCover(vertexCover);
- Console.ReadKey();
- break;
- case 6:
- return;
- }
- }
- }
- public static int[] FindVertexCover(Graph g)
- {
- bool[] visited = new bool[g.NumVertices + 1];
- int[] cover = new int[g.NumVertices];
- int coverSize = 0;
- for (int i = 1; i <= g.NumVertices; i++)
- {
- if (!visited[i])
- {
- visited[i] = true;
- cover[coverSize++] = i;
- var current = g.AdjList[i];
- while (current != null)
- {
- visited[current.Value] = true;
- current = current.Next;
- }
- }
- }
- Array.Resize(ref cover, coverSize);
- return cover;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement