Bob103

C#_Graph

Apr 24th, 2017
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. class Edge
  2.     {
  3.         public readonly Node First;
  4.         public readonly Node Second;
  5.         public Edge(Node first,Node second)
  6.         {
  7.             First = first;
  8.             Second = second;
  9.         }
  10.  
  11.         public bool IsIncident(Node node)
  12.         {
  13.             return node == First || node == Second;
  14.         }
  15.         public Node OtherNode (Node node)
  16.         {
  17.             if (First == node) return Second;
  18.             else if (Second == node) return First;
  19.             else throw new ArgumentException();
  20.         }
  21.     }
  22. /*****************************************************/
  23.  
  24. class Graph
  25.     {
  26.         private Node[] nodes;
  27.         public Graph(int nodesCount)
  28.         {
  29.             nodes = Enumerable
  30.                 .Range(0, nodesCount)
  31.                 .Select(z => new Node(z))
  32.                 .ToArray();
  33.         }
  34.  
  35.         public Node this[int i]
  36.         {
  37.             get { return nodes[i]; }
  38.         }
  39.  
  40.         public IEnumerable<Node> Nodes
  41.         {
  42.             get
  43.             {
  44.                 foreach (var node in nodes)
  45.                 {
  46.                     yield return node;
  47.                 }
  48.             }
  49.         }
  50.        
  51.         public IEnumerable <Edge> Edges
  52.         {
  53.             get
  54.             {
  55.                 return Nodes.SelectMany(z => z.incidentEdges).Distinct();
  56.             }
  57.         }
  58.  
  59.         public static Graph MakeGraph(params int[] incidentNodes)
  60.         {
  61.             var graph = new Graph(incidentNodes.Max() + 1);
  62.             for (int i = 0; i < incidentNodes.Length - 1; i += 2)
  63.                 graph.Connect(incidentNodes[i], incidentNodes[i + 1]);
  64.             return graph;
  65.         }
  66.  
  67.         public void Connect(int v1,int v2)
  68.         {
  69.             nodes[v1].Connect(nodes[v2]);
  70.         }
  71.     }
  72. /*****************************************************/
  73. class Node
  74.     {
  75.         private readonly List<Edge> IncidentEdges = new List<Edge>();
  76.         public readonly int Number;
  77.  
  78.         public Node(int number)//конструктор
  79.         {
  80.             Number = number;
  81.         }
  82.  
  83.         public override string ToString()
  84.         {
  85.             return Number.ToString();
  86.         }
  87.         public IEnumerable<Node> incidentNodes
  88.         {
  89.             get
  90.             {
  91.                 foreach (var edge in IncidentEdges)
  92.                 {
  93.                     yield return edge.OtherNode(this);
  94.                 }
  95.             }
  96.         }
  97.  
  98.         public IEnumerable<Edge> incidentEdges
  99.         {
  100.             get
  101.             {
  102.                 foreach (var edge in incidentEdges)
  103.                 {
  104.                     yield return edge;
  105.                 }
  106.             }
  107.         }
  108.  
  109.         public void Connect (Node anotherNode)
  110.         {
  111.             var edge = new Edge(this, anotherNode);
  112.             IncidentEdges.Add(edge);
  113.             anotherNode.IncidentEdges.Add(edge);
  114.         }
  115.     }
  116. /*****************************************************/
  117.  
  118. class Program
  119.     {
  120.         static List<Node> BreadthSearch(Node startNode)//в ширину
  121.         {
  122.             var res = new List<Node>();
  123.             var queue = new Queue<Node>();
  124.             queue.Enqueue(startNode);
  125.             var visited = new HashSet<Node>();
  126.             while (queue.Count!=0)
  127.             {
  128.                 var node = queue.Dequeue();
  129.                 if (visited.Contains(node)) continue;
  130.                 visited.Add(node);
  131.                 res.Add(node);
  132.  
  133.                 foreach (var nextNode in node.incidentNodes)
  134.                 {
  135.                     queue.Enqueue(nextNode);
  136.                 }
  137.             }
  138.             return res;
  139.         }
  140.  
  141.         static List<Node> DepthSearch(Node startNode)//в глубину
  142.         {
  143.             var res = new List<Node>();
  144.             var stack = new Stack<Node>();
  145.             stack.Push(startNode);
  146.             var visited = new HashSet<Node>();
  147.             while (stack.Count != 0)
  148.             {
  149.                 var node = stack.Pop();
  150.                 if (visited.Contains(node)) continue;
  151.                 visited.Add(node);
  152.                 res.Add(node);
  153.  
  154.                 foreach (var nextNode in node.incidentNodes)
  155.                 {
  156.                     stack.Push(nextNode);
  157.                 }
  158.             }
  159.             return res;
  160.         }
  161.  
  162.         static void Main(string[] args)
  163.         {
  164.             var graph = Graph.MakeGraph(
  165.                 0, 1,
  166.  
  167.                 0, 2,
  168.  
  169.                 1, 3,
  170.  
  171.                 1, 4,
  172.  
  173.                 2, 3,
  174.  
  175.                 2, 4);
  176.  
  177.             //graph.Connect(0, 4);
  178.             foreach (var e in BreadthSearch(graph[0]))
  179.             {
  180.                 Console.WriteLine(e.Number);
  181.             }
  182.         }
  183.     }
Advertisement
Add Comment
Please, Sign In to add comment