当前位置:网站首页>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();
}
}


边栏推荐
- Binary tree pruning
- Openresty installation
- CSDN daily practice -- half search of ordered table
- Deepin20菜单启动选项后自检到iwlwifi停机
- Website online customer service system Gofly source code development log - 5 Gin framework integration daemon
- Bluetooth development (2) -- initialization
- 插入排序
- Bluetooth development (6) -- literacy of Bluetooth protocol architecture
- Error report of curl import postman
- What is the workflow of dry goods MapReduce?
猜你喜欢

【Pygame小遊戲】別找了,休閑遊戲專題來了丨泡泡龍小程序——休閑遊戲研發推薦

Bluetooth development (2) -- initialization

【Pygame小游戏】“史上最炫酷贪吃蛇”驾到,FUN开玩(不好玩不要钱)
![[untitled]](/img/7e/aab9560ef5a1b93f737a82561ec114.png)
[untitled]
![[fireworks in the sky] it's beautiful to light up the night sky with gorgeous fireworks. A programmer brought a fireworks show to pay New Year's greetings to everyone~](/img/3b/2fcd5ff2ea08c4c63428899babd522.png)
[fireworks in the sky] it's beautiful to light up the night sky with gorgeous fireworks. A programmer brought a fireworks show to pay New Year's greetings to everyone~

Wireshake introduction learning notes

Bluetooth development (8) -- avdtp connection process

LabVIEW displays the time and date on the waveform chart or waveform chart

Why is the website snapshot hijacked and tampered with

From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
随机推荐
【 pygame Games 】 don't find, Leisure Games Theme come 丨 Bubble Dragon applet - - Leisure Games Development recommendation
【AcWing】4. Multiple knapsack problem I
Openresty installation
Njupt South Post collection_ Experiment 1
Merge sort
对接请求方式
Easyrecovery15 simple and convenient data recovery tool
示波器和频谱分析仪的区别
Leetcode-15 sum of three numbers
A simple understanding of B tree
SystemVerilog (x) - user defined type
【Pygame小游戏】首月破亿下载 一款高度融合了「超休闲游戏特性」的佳作~
【LaTex】latex VS Code snippets(代码片段)
Shell Sort
[pyGame games] don't look for it. Here comes the leisure game topic - bubble dragon widget - recommendation for leisure game research and development
[MVC&Core]ASP.NET Core MVC 视图传值入门
[pyGame] stir up your brain and play the "24 o'clock" idea together ~ (awesome)
Excel单元格
High speed data stream disk for LabVIEW
yum源更新