[英]How to find the degree of a particular vertex in graph?
import java.util.*;
import java.io.*;
public class homework{
static class Graph {
private LinkedList<Integer> adjLists[];
private boolean visited[];
private int V;
// Graph creation
Graph(int vertices) {
V = vertices;
adjLists = new LinkedList[vertices];
visited = new boolean[vertices];
for (int i = 0; i < vertices; i++)
adjLists[i] = new LinkedList();
}
// Add edges to the graph
void addEdge(int vertices, int dest) {
adjLists[vertices].add(dest);
}
// DFS algorithm
void DFS(int vertex) {
visited[vertex] = true;
System.out.print(vertex + " ");
Iterator<Integer> ite = adjLists[vertex].listIterator();
while (ite.hasNext()) {
int adj = ite.next();
if (!visited[adj])
DFS(adj);
}
}
//BFS algorithm
void BFS(int s) {
boolean visited[] = new boolean[V];
visited[s] = true;
LinkedList<Integer> queue = new LinkedList<>();
queue.add(s);
while (queue.size() != 0) {
s = queue.poll();
System.out.println(s + " ");
Iterator<Integer> i = adjLists[s].listIterator();
while (i.hasNext()) {
int n = i.next();
if (!visited[n]) {
visited[n] = true;
queue.add(n);
}
}
}
}
public static void main(String args[]) {
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is Depth First Traversal");
g.DFS(2);
System.out.println("Flowing is BFS");
g.BFS(2);
int degree = findDegree(G, dest);
System.out.println(degree);
}
}
}
I apologize if I made too many mistakes, but I don't have much experience with java.I have been trying to create a method that find the degree of the vertex .My other methods work like a charm at least for a beginner like me.如果我犯了太多错误,我深表歉意,但我对 java 没有太多经验。我一直在尝试创建一种找到顶点度数的方法。我的其他方法至少对像我这样的初学者来说很有魅力.
static int findDegree(Graph G,int dest){
int degree = 0;
for(int i = 0; i < G.dest;i++){
if(G.dest[i] == 1){
degree++;
}
return degree;
}
}
I tried like this but getting errors that saying (G cannot be resolved to a variable,dest cannot be resolved to a variable).Is there a way to solve this or should i just change the findDegree() method.我试过这样,但收到错误消息(G 无法解析为变量,dest 无法解析为变量)。有没有办法解决这个问题,或者我应该只更改findDegree()方法。
This is such a simple and silly mistake, but it happens with everyone.这是一个如此简单而愚蠢的错误,但它发生在每个人身上。
I think the problem is with the line:我认为问题出在这条线上:
int degree = findDegree(G, dest);
Firstly, change the variable 'G' to 'g' as you have declared small 'g' in your code above.首先,将变量“G”更改为“g”,因为您在上面的代码中声明了小“g”。 Secondly, either initialise dest:
其次,要么初始化 dest:
int dest = 3;
or just pass a number:或者只是传递一个数字:
int degree = findDegree(g, 3);
Hope it helps.希望能帮助到你。
Make an array deg[n] where n is the number of vertices in the graph and whenever you take an edge input x and y, if the graph is undirected then increment deg[x] and deg[y].创建一个数组 deg[n],其中 n 是图中的顶点数,并且每当您输入边输入 x 和 y 时,如果图是无向的,则增加 deg[x] 和 deg[y]。 If the graph is directed then only do deg[x]++.
如果图是有向的,那么只做 deg[x]++。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.