清华大学精品课程网站,网页上做ppt的网站好,页面布局怎么设置,受欢迎的邢台做网站模型图 邻接矩阵
用于反应图中任意两点之间的关联#xff0c;用二维数组表示比较方便
以行坐标为起点#xff0c;列坐标为终点如果两个点之间有边#xff0c;那么标记为绿色#xff0c;如图#xff1a; 适合表示稠密矩阵 邻接表
用一维数组 链表的形式表示#xff…模型图 邻接矩阵
用于反应图中任意两点之间的关联用二维数组表示比较方便
以行坐标为起点列坐标为终点如果两个点之间有边那么标记为绿色如图 适合表示稠密矩阵 邻接表
用一维数组 链表的形式表示以数组下标作为起点链表中的每个节点作为终点形成的邻接表, 如图 适合表示稀疏矩阵 Java代码实现
邻接矩阵 public class AdjacentMatrix {private static Scanner scannernew Scanner(System.in); //扫描器public static void main(String[] args) {System.out.println(------图转换为邻接矩阵------);System.out.println(请输入顶点的数量:);int vertex_count scanner.nextInt();//开辟邻接矩阵boolean[][]adjacentMatrixnew boolean[vertex_count][vertex_count];//初始化矩阵for(int start0;startvertex_count;start){for(int end0;endvertex_count;end){adjacentMatrix[start][end]false;}}//获取边System.out.println(请输入边的数量:);int edge_countscanner.nextInt();System.out.println(请输入这些边的起点和终点,如(start end):);for(int i0;iedge_count;i){int start scanner.nextInt();int end scanner.nextInt();//填充边adjacentMatrix[start][end]true;}//打印输入结果System.out.println(所有边如下:);for (int start0;startvertex_count;start){for(int end0;endvertex_count;end){if(adjacentMatrix[start][end]true)System.out.println(start-end);}}}
}
测试
//输入
------图转换为邻接矩阵------
请输入顶点的数量:
4
请输入边的数量:
5
请输入这些边的起点和终点,如(start end):
2 0
2 1
3 0
3 1
0 1//输出
所有边如下:
0-1
2-0
2-1
3-0
3-1进程已结束退出代码为 0 邻接表
public class AdjacentList {private static class Edge{public Integer endId;public Edge nextEdge;public Edge(Integer endId) {this.endId endId;this.nextEdgenull;}public Edge(Integer endId, Edge nextEdge) {this.endId endId;this.nextEdge nextEdge;}}private static Scanner scannernew Scanner(System.in);public static void main(String[] args) {System.out.println(----------图转换为邻接表----------);System.out.println(请输入顶点的数量:);int vertex_count scanner.nextInt();Edge[]adjacentListnew Edge[vertex_count];System.out.println(请输入边的数量:);int edge_count scanner.nextInt();System.out.println(请输入这些边:);for(int i0;iedge_count;i){int start scanner.nextInt();int end scanner.nextInt();if(adjacentList[start]null)adjacentList[start]new Edge(end);elseadjacentList[start].nextEdgenew Edge(end,adjacentList[start].nextEdge);}System.out.println(邻接表如下:);for (int i 0; i adjacentList.length; i){System.out.print(start:i end:);for(Edge eadjacentList[i];e!null;ee.nextEdge){System.out.print(-e.endId);}System.out.println();}}
}
测试 //输入
----------图转换为邻接表----------
请输入顶点的数量:
4
请输入边的数量:
5
请输入这些边:
2 0
2 1
3 0
3 1
0 1//输出
邻接表如下:
start:0 end:-1
start:1 end:
start:2 end:-0-1
start:3 end:-0-1进程已结束退出代码为 0