IsAdjacencyMatrix.cs

CSharp · Console

/Program.cs

using System;
					
public class Program
{
	static bool IsAdjacencyMatrix(int[,] arr)
	{
		int length = arr.GetLength(0);
		int secondLength = arr.GetLength(1);
		if (length != secondLength)
		{
			throw new Exception("Matrix must be square");
		}
		
		for (int i = 0; i < length; i++)
		{
			for (int j = 0; j < length; j++)
			{
				// проверяем петлю для элементов на главной диагонали
				if (i == j && arr[i, j] == 1)
				{
					return false;
				}
				
				// проверяем симметричность относительно главной диагонали
				if (arr[i, j] != arr[j, i])
				{
					return false;
				}
			}
		}
		return true;
	}
	
	public static void Main()
	{
		Console.WriteLine("UniLecs");
		
		int[,] arr1 = new int[,] { { 0, 1 }, { 1, 0 } };
		int[,] arr2 = new int[,] { { 0, 1, 1 }, { 1, 0, 1 }, { 1, 1, 0 } };
		int[,] arr3 = new int[,] { { 0, 1, 1 }, { 1, 1, 1 }, { 1, 1, 0 } };
		
		Console.WriteLine(string.Format("Answer = {0}", IsAdjacencyMatrix(arr1))); // True
		Console.WriteLine(string.Format("Answer = {0}", IsAdjacencyMatrix(arr2))); // True
		Console.WriteLine(string.Format("Answer = {0}", IsAdjacencyMatrix(arr3))); // False
	}
}
;