数据结构从<;Int>;至<;字符串>;JAVA
这可以完美地对整数进行拓扑排序,但是我想让它与作为参数的字符串类型兼容。有人对我如何从这里更改数据结构有任何指导吗?或者我必须重写整个过程才能使[add.edge(“a”,“b”);]正常工作? 我曾试图简单地更改类型,但后来我感到困惑,一旦我试图更改导致错误的东西,情况就会变得很糟糕。 我试着把所有的都改成这个合适吗?当我改变的越来越多时,我仍然会出错数据结构从<;Int>;至<;字符串>;JAVA,java,Java,这可以完美地对整数进行拓扑排序,但是我想让它与作为参数的字符串类型兼容。有人对我如何从这里更改数据结构有任何指导吗?或者我必须重写整个过程才能使[add.edge(“a”,“b”);]正常工作? 我曾试图简单地更改类型,但后来我感到困惑,一旦我试图更改导致错误的东西,情况就会变得很糟糕。 我试着把所有的都改成这个合适吗?当我改变的越来越多时,我仍然会出错 // A Java program to print topological sorting of a graph // using ind
// A Java program to print topological sorting of a graph
// using indegrees
import java.util.*;
//Class to represent a graph
class Graph
{
int V;// No. of vertices
//An Array of List which contains
//references to the Adjacency List of
//each vertex
List <Integer> adj[];
public Graph(int V){//constructor
this.V = V;
adj = new ArrayList[V];
for(int i = 0; i < V; i++)
adj[i]=new ArrayList<Integer>();
}
// function to add an tasks to tree
public void addEdge(int u,int v){
adj[u].add(v);
}
// prints a Topological Sort of the complete graph
public void topologicalSort(){
// Create a array to store indegrees of all
// vertices. Initialize all indegrees as 0.
int indegree[] = new int[V];
// Traverse adjacency lists to fill indegrees of
// vertices. This step takes O(V+E) time
for(int i = 0; i < V; i++){
ArrayList<Integer> temp = (ArrayList<Integer>) adj[i];
for(int node : temp){
indegree[node]++;
}
}
// Create a queue and enqueue all vertices with
// indegree 0
Queue<Integer> q = new LinkedList<Integer>();
for(int i = 0;i < V; i++){
if(indegree[i]==0)
q.add(i);
}
// Initialize count of visited vertices
int cnt = 0;
// Create a vector to store result (A topological
// ordering of the vertices)
Vector <Integer> topOrder=new Vector<Integer>();
while(!q.isEmpty()){
// Extract front of queue (or perform dequeue)
// and add it to topological order
int u=q.poll();
topOrder.add(u);
// Iterate through all its neighbouring nodes
// of dequeued node u and decrease their in-degree
// by 1
for(int node : adj[u]){
// If in-degree becomes zero, add it to queue
if(--indegree[node] == 0)
q.add(node);
}
cnt++;
}
// Check if there was a cycle
if(cnt != V){
System.out.println("There exists a cycle in the graph");
return ;
}//else{System.out.println("no cycle in the graph");}
// Print topological order
for(int i : topOrder){
System.out.print(i+" ");
}
}
}
// Driver program to test above functions
class Main
{
public static void main(String args[])
{
Graph g=new Graph(8);//must be max number of letters+1
g.addEdge(1, 4);
g.addEdge(2, 5);
g.addEdge(7, 5);
g.addEdge(5, 4);
g.addEdge(3, 4);
g.addEdge(4, 6);
g.addEdge(2, 4);
System.out.println("Following is a Topological Sort");
g.topologicalSort();
}
}
//打印图形拓扑排序的Java程序
//使用索引
导入java.util.*;
//类来表示图形
类图
{
int V;//顶点数
//包含
//对邻接列表的引用
//每个顶点
列表调整[];
公共图(intv){//constructor
这个,V=V;
adj=新阵列列表[V];
对于(int i=0;i
最简单的解决方案是在类的定义中使用泛型。然后,用户可以定义是否需要字符串或整数(或其他任何内容):
Graph-stringGraph=new-Graph();
stringGraph.addEdge(“foo”、“bar”);
Graph intGraph=新图形();
补遗(5,7);
Graph vertexGraph=新图形();
顶点v1=新顶点(…);
顶点v2=新顶点(…);
顶点图。加差(v1,v2);
最简单的解决方案是在类的定义中使用泛型。然后,用户可以定义是否需要字符串或整数(或其他任何内容):
Graph-stringGraph=new-Graph();
stringGraph.addEdge(“foo”、“bar”);
Graph intGraph=新图形();
补遗(5,7);
Graph vertexGraph=新图形();
顶点v1=新顶点(…);
顶点v2=新顶点(…);
顶点图。加差(v1,v2);
What;错在哪里?你尝试了什么?什么;错在哪里?你试了什么?
Graph<String> stringGraph = new Graph<>();
stringGraph.addEdge("foo", "bar");
Graph<Integer> intGraph = new Graph<>();
intGraph.addEdge(5, 7);
Graph<Vertex> vertexGraph = new Graph<>();
Vertex v1 = new Vertex(...);
Vertex v2 = new Vertex(...);
vertexGraph.addEdge(v1, v2);