南邮离散实验四(JAVA)

内容:编程随即生成n个极点的无向图并能进行(半)欧拉图的判定,若是则给出欧拉(回)路。
要求:对给定n个结点,随即生成邻接矩阵以确定某无向简单图并进行欧拉图和半欧拉图的判定,若符合则给出至少一条欧拉回路或欧拉路。

import java.util.Scanner;
import java.util.Stack;

public class Graphtheory {
    public static int spots = 0;
    public static int edges=  0;
    public static int[][] Gate; //表示图的邻接矩阵
    public static int[] visted; //记录点是否被访问0未访问,1已访问
    public static int begin; //判断是否为联通图,begin为查询时的起点
    public static int degree; //顶点的度
    public static int num = 0; //奇度顶点的个数
    public static int start = 0; //欧拉路的起点
    public static Stack stack;
    public static int[] ans; //记录欧拉路的路径
    public static int count = 0; //记录路径数
    public static int maxEdge;

    //随即生成图并输出
    public static void Generate() {
        int cnt = 0;
        while (cnt < edges) {
            int x = (int) (Math.random()*spots); //生成范围是0到n的随机数
            int y = (int) (Math.random()*spots);

            if (x != y && Gate[x][y] == 0) {
                Gate[x][y] = 1;
                Gate[y][x] = 1;
                cnt++;
                begin = x;
            }
        }
        if (spots <= 20) {
            System.out.println("图的邻接矩阵为:");
            for (int i = 0; i < spots; i++) {
                for (int j = 0; j < spots; j++) {
                    System.out.print("  " + Gate[i][j]);
                }
                System.out.println();
            }
            System.out.println();
        }
    }

    //深度遍历
    private static void DFS(int x) {
        visted[x] = 1;
        for (int i = 0; i < spots; i++) {
            if (visted[i] == 0 && Gate[x][i] == 1) {
                DFS(i);
            }
        }
    }

    //判断是否是连通的
    private static boolean Judge() {
        DFS(begin);
        for (int i = 0; i < spots; i++) {
            if (visted[i] == 0) {
                return false;
            }
        }
        return true;
    }

    public static void DFSGraph(int x) {
        stack.push(x);
        for (int i = 0; i < spots; i++) {
            if (Gate[i][x] == 1) {
                Gate[i][x] = 0; //删除该边
                Gate[x][i] = 0;
                DFSGraph(i);
                break;
            }
        }
    }

    //Fleury算法
    public static void Fleury(int x) {
        int flag;
        ans = new int[100];
        stack = new Stack();
        stack.push(x);
        while (!stack.isEmpty()) {
            flag = 0;
            for (int i = 0; i < spots; i++) {
                if (Gate[(int) stack.peek()][i] == 1) {
                    flag = 1;
                    break;
                }
            }
            if (flag == 0) {
                //如果没有可扩展的点,则记录下该点并将其出栈
                ans[count] = 1 + (int)(stack.pop());
                count++;
            }else {
                int n = (int) stack.pop();
                DFSGraph(n);
            }
        }
    }


    public static void main(String[] args) {
        do{
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入无向图顶点的个数(大于0且不超过20的整数)");
            int n = sc.nextInt();
            spots = n;
            System.out.println("请输入边的个数");
            int s = sc.nextInt();
            edges = s;
            maxEdge = n*(n-1)/2;
            if (s > maxEdge) {
                System.out.println("有"+ n + "个点的无向图最多有" + maxEdge+"条边");
            }
        }while (edges > maxEdge);
        Gate = new int[spots][spots];
        visted = new int[spots];
        for (int i = 0; i < spots; i++) {
            visted[i] = 0;
            for (int j = 0; j < spots; j++) {
                Gate[i][j] = 0;

            }
        }
        Generate();
        if (!Judge()) {
            System.out.println("不是连通图");
        }else {
            //如果存在奇度顶点,则从奇度顶点出发,否则从0出发
            for (int i = 0; i < spots; i++) {
                degree = 0;
                for (int j = 0; j < spots; j++) {
                    degree += Gate[i][j];
                }
                if (degree % 2 == 1) {
                    start = i;
                    num++;
                }
            }

            //无向图具有一条欧拉路,当且仅当G是连通的,且有0个或2个奇数度结点
            if (num == 0 || num == 2) {
//                System.out.println("起点为:" + start);
                Fleury(start);
                //欧拉路径的头和尾相等,则说明欧拉路是回路
                if (ans[0] == ans[count - 1]) {
                    System.out.println("是欧拉图,欧拉回路为:");
                }else {
                    System.out.println("是半欧拉图,欧拉路径为:");
                }
                for (int i = 0; i < count; i++) {
                    System.out.print(ans[i]+" ");
                }
            }else {
                System.out.println("不是欧拉图或者半欧拉图");
            }
        }

    }
}

测试结果:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package hamierton; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Random; public class EularCircuit { public EularCircuit() { } public static void main(String[] args) { // System.out.println("please input n:"); // BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = 4; try { // n = Integer.parseInt(br.readLine()); } catch (Exception ex) { return; } try { Graph g = new Graph(n); g.printg(); g.circuit(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); return; } } } class Node { private static int count = 0; private String name; private ArrayList adjacencyList; private boolean visited =false; public Node() { name = "node " + count; adjacencyList = new ArrayList(); } public Node(String name) { this.name = name; adjacencyList = new ArrayList(); } public boolean isAllVisited() { for (int i = 0; i < adjacencyList.size(); i++) { SNode sn = (SNode) adjacencyList.get(i); if (sn.visited == false) { return false; } } return true; } public boolean isvisited(){ return visited; } public void setvisited(){ visited = true; } public int getAdjacencyCount() { return adjacencyList.size(); } public boolean contains(int i) { return this.adjacencyList.contains(new SNode(i)); } public void removeAdjacencyNode(int i) { this.adjacencyList.remove(new SNode(i)); } public void addAdjacencyNode(int i) { this.adjacencyList.add(new SNode(i)); } public SNode getAdjacencyNode(int i) { return (SNode) this.adjacencyList.get(i); } public SNode getAdjacencyNodeEX(int i_ref) { for (int i = 0; i < this.getAdjacencyCount(); i++) { if (getAdjacencyNode(i).index == i_ref) { return getAdjacencyNode(i); } } return null; } public String toString() { return this.name; } } class SNode { public boolean visited = false; public int index = 0; public SNode(int index) { this.index = index; } public boolean equals(Object o) { if (((SNode) o).index == this.index) { return true; } return false; } public String toString() { return "adjacency " + index; } } class Graph { private ArrayList nodeList; private ArrayList path; private int count; public Graph(int n) throws Exception { this.count = n; nodeList = new ArrayList(count); ginit(); } public void circuit() { path = new ArrayList(); int top = 0; int k = 0; path.add(new Integer(0)); while (true) { int i, j; i = top; ArrayList path1 = new ArrayList(); path1.add(new Integer(top)); while (true) { Node node = (Node) nodeList.get(i); for (j = 0; j < this.count; j++) { if (node.contains(j) && node.getAdjacencyNodeEX(j).visited == false) { path1.add(new Integer(j)); node.getAdjacencyNodeEX(j).visited = true; // ((Node) nodeList.get(j)).getAdjacencyNodeEX(i).visited = true; i = j; break; } } if (i == top) { break; } } path.remove(k); path.addAll(k, path1); k++; if (k >= path.size()) { break; } top = ((Integer) path.get(k)).intValue(); } for (int z = 0; z < path.size(); z++) { System.out.print(path.get(z).toString() + " "); } } private void ginit() { int i; for (i = 0; i < 4; i++) { nodeList.add(new Node("node" + i)); } ((Node)nodeList.get(0)).addAdjacencyNode(3); ((Node)nodeList.get(1)).addAdjacencyNode(0); ((Node)nodeList.get(2)).addAdjacencyNode(1); ((Node)nodeList.get(3)).addAdjacencyNode(2); // ((Node)nodeList.get(0)).addAdjacencyNode(3); // ((Node)nodeList.get(1)).addAdjacencyNode(0); // ((Node)nodeList.get(2)).addAdjacencyNode(1); // ((Node)nodeList.get(3)).addAdjacencyNode(2); // for (i = 0; i < n; i++) { // nodeList.add(new Node("node" + i)); // } // ArrayList linked = new ArrayList(); // linked.add(new Integer(0)); // Random rand = new Random(); // // for (i = 1; i < n; i++) { // int size = linked.size(); // // int top = ((Integer) (linked.get(size - 1))).intValue(); // Node node = (Node) (nodeList.get(top)); // // while (true) { // int randint = rand.nextInt(n); // if (randint == top) { // continue; // } // if (node.contains(randint)) { // continue; // } else { // if (!linked.contains(new Integer(randint))) { // linked.add(new Integer(randint)); // } else if (node.getAdjacencyCount() >= (linked.size() - 1 > 6 ? 6 // : linked.size() - 1)) { // continue; // } else { // i--; // } // node.addAdjacencyNode(randint); // Node randnode = (Node) nodeList.get(randint); // randnode.addAdjacencyNode(top); // break; // } // } // } // // for (i = 0; i < this.count - 1; i++) { // Node node = (Node) nodeList.get(i); // if (node.getAdjacencyCount() % 2 != 0) { // int j = 0; // for (j = i + 1; j < this.count; j++) { // Node nn = (Node) nodeList.get(j); // if (nn.getAdjacencyCount() % 2 != 0) { // if (node.contains(j)) { // // if (nn.getAdjacencyCount() != 1 // && node.getAdjacencyCount() != 1) { // node.removeAdjacencyNode(j); // nn.removeAdjacencyNode(i); // break; // } else { // continue; // } // } else { // // node.addAdjacencyNode(j); // nn.addAdjacencyNode(i); // break; // } // } // } // // if (j == this.count) { // int k; // Node nk = null; // for (k = i + 1; k < this.count; k++) { // // nk = (Node) nodeList.get(k); // if (nk.getAdjacencyCount() % 2 != 0) { // break; // } // } // int kk = k; // for (k = 0; k < i; k++) { // Node n1 = (Node) nodeList.get(k); // if (!n1.contains(kk) && !n1.contains(i)) { // n1.addAdjacencyNode(kk); // nk.addAdjacencyNode(k); // n1.addAdjacencyNode(i); // node.addAdjacencyNode(k); // break; // } // } // boolean retry = false; // // if (k == i) { // int vv; // for (vv = 0; vv < this.count; vv++) { // Node vn = (Node) nodeList.get(vv); // if (!vn.contains(i) && i != vv) { // vn.addAdjacencyNode(i); // node.addAdjacencyNode(vv); // retry = true; // break; // } // } // if (vv == count) { // for (vv = 0; vv < count; vv++) { // Node vnn = (Node) nodeList.get(vv); // if (vnn.getAdjacencyCount() > 1) { // vnn.removeAdjacencyNode(i); // node.removeAdjacencyNode(vv); // retry = true; // break; // } // } // } // } // if (retry) { // i = -1; // } // } // } // // } // return this.isEularG(); } public boolean isEularG() { boolean isEular = true; for (int i = 0; i < this.count; i++) { Node n = (Node) nodeList.get(i); if (n.getAdjacencyCount() % 2 != 0) { isEular = false; break; } } return isEular; } public void printg() { for (int i = 0; i < this.count; i++) { Node n = (Node) nodeList.get(i); System.out.print(n.toString() + " "); for (int j = 0; j < n.getAdjacencyCount(); j++) { System.out.print(n.getAdjacencyNode(j).toString() + " "); } System.out.println(); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值