Fix the graph to be Eulerian

I was trying to make a uni project that generates graph with given values and makes it eulerian if it’s not but the supposed fixed graph still has odd degree verticies.Please help me find the problem.Probably in FixGraph().It’s repearing the stuff a bit but it still doesn’t solve the problem entirely (some veritices still odd).

using System;
using System.Collections.Generic;

public class Graph
{
    private int V;   // No. of vertices
    private List<int>[] adj; // Array of lists for Adjacency List Representation
    private Random rand;

    // Constructor
    public Graph(int v)
    {
        V = v;
        adj = new List<int>[v];
        for (int i = 0; i < v; ++i)
            adj[i] = new List<int>();
        rand = new Random();
    }

    // Function to add an edge into the graph
    public void addEdge(int v, int w)
    {
        adj[v].Add(w);// Add w to v's list.
        adj[w].Add(v); //The graph is undirected
    }

    // Function to generate the graph based on probability
    public void GenerateGraph(double probability)
    {
        for (int i = 0; i < V; i++)
        {
            for (int j = i + 1; j < V; j++)
            {
                if (rand.NextDouble() < probability)
                {
                    addEdge(i, j);
                }
            }
        }
    }

    // Function to display the adjacency matrix
    public void DisplayAdjacencyMatrix()
    {
        Console.WriteLine("Adjacency Matrix:");
        for (int i = 0; i < V; i++)
        {
            for (int j = 0; j < V; j++)
            {
                if (adj[i].Contains(j))
                    Console.Write("1 ");
                else
                    Console.Write("0 ");
            }
            Console.WriteLine();
        }
    }

    // A function used by DFS
    private void DFSUtil(int v, bool[] visited)
    {
        // Mark the current node as visited
        visited[v] = true;

        // Recur for all the vertices adjacent to this vertex
        foreach (int neighbor in adj[v])
        {
            int n = neighbor;
            if (!visited[n])
                DFSUtil(n, visited);
        }
    }

    // Method to check if all non-zero degree vertices are connected. It mainly does DFS traversal starting from
    private bool IsConnected()
    {
        // Mark all the vertices as not visited
        bool[] visited = new bool[V];
        for (int i = 0; i < V; i++)
            visited[i] = false;

        // Find a vertex with non-zero degree
        int j;
        for (j = 0; j < V; j++)
            if (adj[j].Count != 0)
                break;

        // If there are no edges in the graph, return true
        if (j == V)
            return true;

        // Start DFS traversal from a vertex with non-zero degree
        DFSUtil(j, visited);

        // Check if all non-zero degree vertices are visited
        for (int k = 0; k < V; k++)
            if (!visited[k] && adj[k].Count > 0)
                return false;

        return true;
    }

    // Function to fix the graph by connecting vertices with odd degrees
    public void FixGraph()
    {
        // Find vertices with odd degree
        List<int> oddVertices = new List<int>();
        for (int i = 0; i < V; i++)
        {
            if (adj[i].Count % 2 != 0)
                oddVertices.Add(i);
        }

        // Connect vertices with odd degree
        for (int i = 0; i < oddVertices.Count; i += 2)
        {
            if (i + 1 < oddVertices.Count)
                addEdge(oddVertices[i], oddVertices[i + 1]);
        }
    }

    // Method to run test cases
    public void Test()
    {
        DisplayAdjacencyMatrix();
        int res = IsEulerian();
        if (res == 0)
        {
            Console.WriteLine("Graph is not Eulerian, fixing it...");
            FixGraph();
            Console.WriteLine("Fixed graph:");
            DisplayAdjacencyMatrix();
        }
        else if (res == 1)
            Console.WriteLine("Graph has an Euler path");
        else
            Console.WriteLine("Graph has an Euler cycle");
    }

    // Method to check if the graph is Eulerian
    private int IsEulerian()
    {
        // Check if all non-zero degree vertices are connected
        if (!IsConnected())
            return 0;

        // Count vertices with odd degree
        int odd = 0;
        for (int i = 0; i < V; i++)
            if (adj[i].Count % 2 != 0)
                odd++;

        // If count is more than 2, then graph is not Eulerian
        if (odd > 2)
            return 0;

        // If odd count is 2, then semi-eulerian.
        // If odd count is 0, then eulerian
        // Note that odd count can never be 1 for undirected graph
        return odd == 2 ? 1 : 2;
    }

    // Driver method
    public static void Main(string[] args)
    {
        // Let us create and test graphs
        int vertices = 10;
        double probability = 0.5; // Adjust the probability as needed

        Graph g = new Graph(vertices);
        g.GenerateGraph(probability);
        g.Test();

        // Displaying edges
        Console.WriteLine("nEdges:");
        for (int i = 0; i < vertices; i++)
        {
            foreach (var neighbor in g.adj[i])
            {
                if (i < neighbor)
                    Console.WriteLine($"{i}  {neighbor}");
            }
        }
    }
}

everythin on the internet

New contributor

pkrzysiek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật