- import java.io.File;
- import java.util.Scanner;
- public class GraphReverse {
- int arc[][]; //存放图中边的数组
- int v,e; //图的定点数和边数
- int[] visited;
- int flag=0;
- public GraphReverse(int v, int e){
- this.v = v;
- this.e = e;
- arc = new int[v][v];
- for(int i=0; i<v; i++)
- for(int j=0; j<v; j++){
- arc[i][j] = 0;
- }
- visited = new int[v];
- for(int i=0; i<v; i++){
- visited[i] = 0;
- }
- }
- public void edges(int v1,int v2){ //图中边的信息
- arc[v1][v2] = 1;
- arc[v2][v1] = -1;
- }
- public int[][] getEdges(){ //获取邻接矩阵
- return arc;
- }
- public void ALGraph(){ //原图邻接表
- for(int i=0; i<v; i++){
- System.out.print(i+":");
- for(int j=0; j<v; j++){
- if(arc[i][j]==1){
- System.out.print(j+" ");
- }
- }
- System.out.println();
- }
- }
- public void Graph(){ //反向图邻接表
- for(int i=0; i<v; i++){
- System.out.print(i+":");
- for(int j=0; j<v; j++){
- if(arc[i][j]==-1){
- System.out.print(j+" ");
- }
- }
- System.out.println();
- }
- }
- public static void main(String[] args) throws Exception {
- int i,j;
- Scanner scanner = new Scanner(new File("tinyDG.txt"));
- int v = scanner.nextInt(); //顶点数
- int e = scanner.nextInt(); //边数
- GraphReverse g = new GraphReverse(v, e);
- for(i=0; i<e; i++){
- int v1 = scanner.nextInt();
- int v2 = scanner.nextInt();
- g.edges(v1, v2);
- }
- g.ALGraph();
- System.out.println("反向图的邻接表:");
- g.Graph();
- }
- }
04-08
1445
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交