gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
реализуй структуру данных граф на языку c#
- User
реализуй структуру данных граф на языку c#
- Assistant
Вот пример реализации структуры данных граф на языке C#:
using System; using System.Collections.Generic; public class Graph<T> { private Dictionary<T, List<T>> adjacencyList; public Graph() { adjacencyList = new Dictionary<T, List<T>>(); } public void AddVertex(T vertex) { if (!adjacencyList.ContainsKey(vertex)) { adjacencyList[vertex] = new List<T>(); } } public void AddEdge(T vertex1, T vertex2) { if (!adjacencyList.ContainsKey(vertex1)) { adjacencyList[vertex1] = new List<T>(); } if (!adjacencyList.ContainsKey(vertex2)) { adjacencyList[vertex2] = new List<T>(); } adjacencyList[vertex1].Add(vertex2); adjacencyList[vertex2].Add(vertex1); } public List<T> GetNeighbours(T vertex) { if (adjacencyList.ContainsKey(vertex)) { return adjacencyList[vertex]; } else { throw new KeyNotFoundException(); } } }