当前位置:网站首页>Njuptn Nanyou Discrete Mathematics_ Experiment 4
Njuptn Nanyou Discrete Mathematics_ Experiment 4
2022-06-11 00:10:00 【Retreat drum ten level performer】
Content :
Yes, yes. n An undirected graph with nodes , Judge whether it can be drawn by one stroke .
requirement :
For given n An undirected graph with nodes , Determine the Eulerian graph and semi Eulerian graph , If it is an Euler or semi Euler , Then all Euler ( return ) road .
package exp04;
import java.util.Stack;
class Graph {
int[][] matrix; // The adjacency matrix of a graph
int n; // Number of vertices
boolean[] visted; // Mark whether the point is accessed
Stack<Integer> s=new Stack(); // Stack
int[] ans=new int[50];
int count = 0; // Record the path of Euler Road , Number of paths
public Graph(int[][] matrix, int n) {
this.matrix = matrix;
this.n = n;
visted = new boolean[n];
for (boolean v: visted) {
v=false;
}
}
// Traverse the graph first from the set starting depth , If a point is not visited , Then it is a non connected graph
public boolean Judge()
{
DFS(0);
for(int i = 0; i < n; i++)
if(!visted[i])
return false;
return true;
}
// Depth-first search
private void DFS(int x)
{
visted[x] = true;
for(int i = 0; i < n; i++)
if(!visted[i] && matrix[x][i]==1)
DFS(i);
}
public void JudgeEuler(){
int degree,start=0,num=0;
for(int i = 0; i < n; i++) // If there are singular vertices , Then start from the vertex of singularity , Otherwise, from 0 set out
{
degree = 0;
for(int j = 0; j < n; j++)
degree += matrix[i][j];
if(degree % 2!=0)
{
start = i;
num++;
}
}
// An undirected graph has an Euler path , If and only if G It's connected , And there are 0 Or 2 Odd degree nodes
if(num == 0 || num == 2)
{
Fleury(start);
// The head and tail of Euler's path are equal , It means that Euler road is a circuit
if(ans[0] == ans[count - 1])
System.out.println(" The graph is an Euler graph , Euler loop is :");
else
System.out.println(" The graph is a semi Eulerian graph , Eulaluwei :");
Answer();
}
else
System.out.println(" This graph is not an Eulerian or semi Eulerian graph ");
}
private void Answer() {
for(int i = 0; i < count; i++)
System.out.print(ans[i]+" ");
System.out.println();
}
private void Fleury(int x) {
int flag;
s.push(x);
while(!s.isEmpty())
{
flag = 0;
for(int i = 0; i < n; i++)
{
if(matrix[s.lastElement()][i] > 0)
{
flag = 1;
break;
}
}
if(flag == 0) // If there are no extensible points , Then record the point and put it out of the stack
{
ans[count ++] = s.pop() + 1;
}
else // If there is , Then it will be out of the stack and continue to search
{
DFSGraph(s.pop());
}
}
}
private void DFSGraph(int x)
{
s.push(x);
for(int i = 0; i < n; i++)
{
if(matrix[i][x] > 0)
{
matrix[i][x] = 0; // Delete the side
matrix[x][i] = 0;
DFSGraph(i);
break;
}
}
}
}
package exp04;
import java.awt.*;
import java.util.Scanner;
public class Test04 {
public static void main(String[] args) {
System.out.println(" Please enter the number of undirected graph nodes :");
Scanner input = new Scanner(System.in);
int n=input.nextInt(); // Number of undirected graph nodes
System.out.println(" Please enter the adjacency matrix of the undirected graph to be determined :");
int[][] matrix=new int[n][n];
System.out.print(" ");
for (int i = 1; i <=n; i++) {
System.out.print(" "+i);
}
System.out.println();
for (int i = 0; i < n; i++) {
// The adjacency matrix of the input graph
System.out.print(i+1);
for (int j = 0; j < n; j++) {
matrix[i][j]=input.nextInt();
}
}
Graph g = new Graph(matrix,n);
System.out.println(g.Judge()?" This graph is connected ":" This graph is unconnected ");
if (g.Judge()==false) // If it's a non connected graph , No further judgment is required
return;
g.JudgeEuler();
}
}


边栏推荐
- 【Go语言学习】——并发编程
- Chapter 2 application layer 2.4 DNS
- 【Pygame合集】滴~穿越童年游戏指南 请查收:这里面有你玩过的游戏嘛?(附五款源码自取)
- Openresty installation
- 【Pygame小游戏】剧情流推荐:什么样的游戏才能获得大家的喜欢呢?(魔鬼恋人、霸总娇妻版)
- Bluetooth development (7) -- L2CAP layer connection process
- 启牛学堂理财可靠吗,安全吗
- ASP. Net programming version C (notes along with learning progress)
- How to check the variable waveform when debugging the program? Look here
- C language file operation
猜你喜欢

【无标题】

Kubernetes 基本介绍及核心组件
![[AI card player] for the first time to see such an](/img/7b/f1965a74733e152dcc542e465112b0.png)
[AI card player] for the first time to see such an "exciting" landowner, the key factor for a high winning rate is

From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
![[pyGame collection] memory killing -](/img/97/10a4333662b49ac35e5b7433a5e6a4.png)
[pyGame collection] memory killing - "Childhood Games", how many shots did you get? (attach five source codes for self access)

SystemVerilog (x) - user defined type

Wireshake introduction learning notes

【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)

【Pygame小游戏】首月破亿下载 一款高度融合了「超休闲游戏特性」的佳作~

Bluetooth development (8) -- avdtp connection process
随机推荐
MultipartFile重命名上传
集合删除元素技巧 removeIf
[appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)
After deepin20 menu startup option, the self-test indicates that iwlwwifi is stopped
SystemVerilog (x) - user defined type
[pyGame games] in the first month, it broke 100 million to download a masterpiece that is highly integrated with "super casual game features"~
[pyGame games] here it is. This Gobang game is super A. share it with your friends~
ASP. Net programming version C (notes along with learning progress)
归并排序
B 树的简单认识
phpstudy的安装
IGBT and third generation semiconductor SiC double pulse test scheme
启牛学堂理财可靠吗,安全吗
安全生产月,黄埔开展燃气安全进商铺宣传活动
【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
示波器和频谱分析仪的区别
Typecho blog site wide deployment of Tencent cloud CDN tutorial - Xingze V Club
What is the workflow of dry goods MapReduce?
Basic introduction and core components of kubernetes
2022 college entrance examination quantitative paper | please answer the questions for quantitative candidates