mardi 28 janvier 2020

Randomly Generated Graphs to output Euler path or Euler cycle for it

I have this code in Java. It can print an Euler path or a cycle for a given graphs using Fleury’s algorithm. My goal is to modify this code to print Euler paths or circuits using only randomly generated graphs. In addition I would like to make it print only minimal Euler cycles, but the main goal is random generation fo graphs.

import java.util.ArrayList; 

public class Graph { 

    private int vertices; 
    private ArrayList<Integer>[] adj; 

    Graph(int numOfVertices) 
    { 
        this.vertices = numOfVertices; 

        initGraph(); 
    } 

    @SuppressWarnings("unchecked") 
    private void initGraph() 
    { 
        adj = new ArrayList[vertices]; 
        for (int i = 0; i < vertices; i++)  
        { 
            adj[i] = new ArrayList<>(); 
        } 
    } 

    private void addEdge(Integer u, Integer v) 
    { 
        adj[u].add(v); 
        adj[v].add(u); 
    } 

    private void removeEdge(Integer u, Integer v) 
    { 
        adj[u].remove(v); 
        adj[v].remove(u); 
    } 

    private void printEulerTour() 
    { 
        Integer u = 0; 
        for (int i = 0; i < vertices; i++) 
        { 
            if (adj[i].size() % 2 == 1) 
            { 
                u = i; 
                break; 
            } 
        } 

        printEulerUtil(u); 
        System.out.println(); 
    } 

    private void printEulerUtil(Integer u) 
    { 
        for (int i = 0; i < adj[u].size(); i++) 
        { 
            Integer v = adj[u].get(i); 
            if (isValidNextEdge(u, v))  
            { 
                System.out.print(u + "-" + v + " "); 

                removeEdge(u, v);  
                printEulerUtil(v); 
            } 
        } 
    } 

    private boolean isValidNextEdge(Integer u, Integer v) 
    { 
        if (adj[u].size() == 1) { 
            return true; 
        } 

        boolean[] isVisited = new boolean[this.vertices]; 
        int count1 = dfsCount(u, isVisited); 

        removeEdge(u, v); 
        isVisited = new boolean[this.vertices]; 
        int count2 = dfsCount(u, isVisited); 

        addEdge(u, v); 
        return (count1 > count2) ? false : true; 
    } 

    private int dfsCount(Integer v, boolean[] isVisited) 
    { 
        isVisited[v] = true; 
        int count = 1; 
        for (int adj : adj[v]) 
        { 
            if (!isVisited[adj]) 
            { 
                count = count + dfsCount(adj, isVisited); 
            } 
        } 
        return count; 
    } 

    public static void main(String a[]) 
    {  
        //My graphs

        Graph g1 = new Graph(5); 
        g1.addEdge(1, 0); 
        g1.addEdge(0, 2); 
        g1.addEdge(2, 1); 
        g1.addEdge(0, 3); 
        g1.addEdge(3, 4); 
        g1.addEdge(3, 2); 
        g1.addEdge(3, 1); 
        g1.addEdge(2, 4);
        g1.printEulerTour(); 
    } 
}

Any ideas how can I implement random generation for graphs here?




Aucun commentaire:

Enregistrer un commentaire