Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///C# shit
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Курсовая_часть_3
- {
- /*TODO
- * НАХОЖДЕНИЕ ПАРОСОЧЕТАНИЙ
- * МАКСИМАЛЬНОЕ ПАРОСОЧЕТАНИЕ
- * */
- //
- /**gotta get a grip
- to find permanent we need a matrix like this:
- 1 0 0 0 0 0
- 0 1 1 0 0 0
- 0 1 1 0 0 0
- 0 1 1 0 0 0
- 0 1 1 0 0 0
- 1 0 1 1 1 1
- if we want to make a big matrix from this it will look like this:
- 0 0 0 0 0 0|1 0 0 0 0 0
- 0 0 0 0 0 0|0 1 1 0 0 0
- 0 0 0 0 0 0|0 1 1 0 0 0
- 0 0 0 0 0 0|0 1 1 0 0 0
- 0 0 0 0 0 0|0 1 1 0 0 0
- 0 0 0 0 0 0|1 0 1 1 1 1
- -----------------------
- 1 0 0 0 0 1|0 0 0 0 0 0
- 0 1 1 1 1 0|0 0 0 0 0 0
- 0 1 1 1 1 0|0 0 0 0 0 0
- 0 0 0 0 0 1|0 0 0 0 0 0
- 0 0 0 0 0 1|0 0 0 0 0 0
- 0 0 0 0 0 1|0 0 0 0 0 0
- right upper square is our part for finding permanent
- left down sqaure is the transponent right upper
- */
- class Graph
- {
- int[,] Matrix;
- //int[][] Matrix2;
- int amount;
- int sum;
- const double EPS = 1E-9;
- public Graph()
- {
- amount = 0;
- Matrix = new int[0, 0];
- sum = 0;
- }
- public int[,] CreateGraph()
- {
- Console.WriteLine("Enter the amount of vertexes");
- amount = int.Parse(Console.ReadLine())/2;
- Matrix = new int[amount, amount];
- for (int i = 0; i < amount; i++)
- {
- Console.WriteLine("Enter new row of matrix, separating values by space");
- string[] str = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- for (int j = 0; j < amount; j++)
- {
- int.TryParse(str[j], out Matrix[i, j]);
- }
- Console.WriteLine();
- }
- return Matrix;
- }
- public void Display()
- {
- Console.WriteLine("Matrix:");
- for (int i = 0; i < amount; i++)
- {
- for (int j = 0; j < amount; j++)
- {
- Console.Write("|" + Matrix[i, j] + "|");
- }
- Console.WriteLine();
- Console.WriteLine("-------------------------");
- }
- Console.WriteLine("Permanent is: {0}", permanent(Matrix));
- }
- public static int[,] minor(int[,] A, int x, int y)
- {
- int length = A.GetLength(0) - 1;
- //Console.WriteLine(length);
- int[,] result = new int[length, length];
- for (int i = 0; i < length; i++)
- {
- for (int j = 0; j < length; j++)
- {
- if (i < x && j < y)
- {
- result[i, j] = A[i, j];
- }
- else if (i >= x && j < y)
- {
- result[i, j] = A[i + 1, j];
- }
- else if (i < x && j >= y)
- {
- result[i, j] = A[i, j + 1];
- }
- else
- {
- result[i, j] = A[i + 1, j + 1];
- }
- }
- }
- return result;
- }
- public int permanent(int[,] A)
- {
- if(A.GetLength(0) == 1){
- return A[0,0];
- }
- else{
- for (int i = 0; i < A.GetLength(0); i++)
- {
- for (int j = 0; j < A.GetLength(0); j++)
- {
- Console.Write("|" + A[i, j] + "|");
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- for (int i =0; i<A.GetLength(0); i++){
- sum += A[0,i] * permanent(minor(A,0,i)); //A in brackets
- }
- }
- return sum;
- }
- }
- //create class permanent or паросочетание
- public static class ArrayExtensions
- {
- public static T[] GetRow<T>(this T[,] data, int i)
- {
- T[] row = new T[data.GetLength(1)];
- //T[] row = new T[data.GetLength(i)];
- for (int j = 0; j < /*data.GetLength(i)*/ row.Length; j++)
- {
- row[j] = data[i, j];
- }
- return row;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Graph dvydol = new Graph();
- dvydol.CreateGraph();
- dvydol.Display();
- Console.ReadKey();
- }
- }
- }
- ///Java Rosetta
- import java.util.Scanner;
- public class MatrixArithmetic {
- public static double[][] minor(double[][] a, int x, int y){
- int length = a.length-1;
- double[][] result = new double[length][length];
- for(int i=0;i<length;i++) for(int j=0;j<length;j++){
- if(i<x && j<y){
- result[i][j] = a[i][j];
- }else if(i>=x && j<y){
- result[i][j] = a[i+1][j];
- }else if(i<x && j>=y){
- result[i][j] = a[i][j+1];
- }else{ //i>x && j>y
- result[i][j] = a[i+1][j+1];
- }
- }
- return result;
- }
- public static double perm(double[][] a){
- if(a.length == 1){
- return a[0][0];
- }else{
- double sum = 0;
- for(int i=0;i<a.length;i++){
- sum += a[0][i] * perm(minor(a,0,i));
- }
- return sum;
- }
- }
- public static void main(String args[]){
- Scanner sc = new Scanner(System.in);
- int size = sc.nextInt();
- double[][] a = new double[size][size];
- for(int i=0;i<size;i++) for(int j=0;j<size;j++){
- a[i][j] = sc.nextDouble();
- }
- sc.close();
- System.out.println("Determinant: "+det(a));
- System.out.println("Permanent: "+perm(a));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement