import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class AdjMatrix {
private int V;
private int E;
private int[][] adj;
// 构造函数,从文件内容初始化邻接矩阵
public AdjMatrix(String filename){
File file = new File(filename);
try(Scanner scanner = new Scanner(file)){
V = scanner.nextInt(); //读取第一行第一个数
if(V < 0) throw new IllegalArgumentException("V must be non-negative");
adj = new int[V][V]; //构造邻接矩阵
if(E < 0) throw new IllegalArgumentException("E must be non-negative");
E = scanner.nextInt(); //读取第一行第二个数
// E是边的数量,在g.txt中表示为第二行开始的E行
for (int i = 0; i < E; i++) {
int a = scanner.nextInt(); //读取边的一个顶点
validateVertex(a);
int b = scanner.nextInt(); //读取边的另一个顶点
validateVertex(b);
if(a == b) throw new IllegalArgumentException("Self Loop is Detected"); //判断是够存在自环边
if(adj[a][b] == 1) throw new IllegalArgumentException("Parallel Edges are Detected"); //判断是够存在平行l边
adj[a][b] = 1; //存在边则设置为1
adj[b][a] = 1;
}
}
catch (IOException e){
e.printStackTrace();
}
}
private void validateVertex(int v){
if(v < 0 || v >= V)
throw new IllegalArgumentException("vertex" + v + "is invalid");
}
public int V(){
return V;
}
public int E(){
return E;
}
public boolean hasEdge(int v, int w){
validateVertex(v);
validateVertex(w);
return adj[v][w] == 1;
}
public ArrayList<Integer> adj(int v){
validateVertex(v);
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < V; i++) {
if(adj[v][i] == 1) res.add(i);
}
return res;
}
public int degree(int v){
return adj(v).size(); //adj(v)是上方的adj方法,size()是ArrayList的接口
}
// 用于在控制台打印该临接矩阵
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(String.format("V = %d, E = %d\n", V, E)); //打印顶点数和边的数量
for (int i = 0; i < V; i++) { //行
for (int j = 0; j < V; j++) { //列
sb.append(String.format("%d",adj[i][j])); //读取矩阵的值
}
sb.append('\n'); //行尾换行
}
return sb.toString(); //返回该邻接矩阵
}
public static void main(String[] args){
AdjMatrix adjMatrix = new AdjMatrix("g.txt"); //新建邻接矩阵,并从文件内容初始化
System.out.println(adjMatrix);
}
}
空间复杂度:O(V^2)
时间复杂度分析:
建图:O(E)
查看两点是否相邻:O(1)
求一个点的相邻节点:O(V)
如果一个图有3000个节点,空间需要3000^2的空间
稀疏图和稠密图: 稀疏和稠密指的是边的多少。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Scanner;
public class AdjList {
private int V;
private int E;
private LinkedList<Integer>[] adj;
public AdjList(String filename){
File file = new File(filename);
try(Scanner scanner = new Scanner(file)){
V = scanner.nextInt();
if(V < 0) throw new IllegalArgumentException("V must be non-negative");
adj = new LinkedList[V]; //构造邻接表, V行,V个LinkedList
for (int i = 0; i < V; i++) {
adj[i] = new LinkedList<Integer>();
}
E = scanner.nextInt(); //读取第一行第二个数
if(E < 0) throw new IllegalArgumentException("E must be non-negative");
// E是边的数量,在g.txt中表示为第二行开始的E行
for (int i = 0; i < E; i++) {
int a = scanner.nextInt(); //读取边的一个顶点
validateVertex(a);
int b = scanner.nextInt(); //读取边的另一个顶点
validateVertex(b);
if(a == b) throw new IllegalArgumentException("Self Loop is Detected"); //判断是够存在自环边
if(adj[a].contains(b)) throw new IllegalArgumentException("Parallel Edges are Detected"); //判断是够存在平行l边
adj[a].add(b);
adj[b].add(a);
}
}
catch (IOException e){
e.printStackTrace();
}
}
private void validateVertex(int v){
if(v < 0 || v >= V)
throw new IllegalArgumentException("vertex" + v + "is invalid");
}
public int V(){
return V;
}
public int E(){
return E;
}
public boolean hasEdge(int v, int w){
validateVertex(v);
validateVertex(w);
return adj[v].contains(w);
}
public LinkedList<Integer> adj(int v){
validateVertex(v);
return adj[v];
}
public int degree(int v){
return adj(v).size();
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(String.format("V = %d, E = %d\n", V, E));
for (int i = 0; i < V; i++) {
sb.append(String.format("%d:",i));
for (int w: adj[i]) {
sb.append(String.format("%d ",w));
}
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args){
AdjList adjList = new AdjList("g.txt"); //新建邻接矩阵,并从文件内容初始化
System.out.println(adjList);
}
}
空间复杂度:O(V+E)
时间复杂度:
建图:O(E*V)
查看两点是否相邻:O(degree(v))
求一个点的相邻节点:O(degree(v))
突破时间瓶颈:
快速查重
快速查看两点是否相邻
解决方案:不适用链表,使用哈希表Hashset ( O(1) ) 或者红黑树TreeSet ( O(logV) ).
改为使用红黑树,可以保证数据的顺序,并且时间复杂度相对小
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.TreeSet;
public class AdjSet {
private int V;
private int E;
private TreeSet<Integer>[] adj;
public AdjSet(String filename){
File file = new File(filename);
try(Scanner scanner = new Scanner(file)){
V = scanner.nextInt();
if(V < 0) throw new IllegalArgumentException("V must be non-negative");
adj = new TreeSet[V]; //构造邻接表, V行,V个LinkedList
for (int i = 0; i < V; i++) {
adj[i] = new TreeSet<Integer>();
}
E = scanner.nextInt(); //读取第一行第二个数
if(E < 0) throw new IllegalArgumentException("E must be non-negative");
// E是边的数量,在g.txt中表示为第二行开始的E行
for (int i = 0; i < E; i++) {
int a = scanner.nextInt(); //读取边的一个顶点
validateVertex(a);
int b = scanner.nextInt(); //读取边的另一个顶点
validateVertex(b);
if(a == b) throw new IllegalArgumentException("Self Loop is Detected"); //判断是够存在自环边
if(adj[a].contains(b)) throw new IllegalArgumentException("Parallel Edges are Detected"); //判断是够存在平行l边
adj[a].add(b);
adj[b].add(a);
}
}
catch (IOException e){
e.printStackTrace();
}
}
private void validateVertex(int v){
if(v < 0 || v >= V)
throw new IllegalArgumentException("vertex" + v + "is invalid");
}
public int V(){
return V;
}
public int E(){
return E;
}
public boolean hasEdge(int v, int w){
validateVertex(v);
validateVertex(w);
return adj[v].contains(w);
}
public Iterable<Integer> adj(int v){ // 可以是TreeSet,但是数组、链表、红黑树都是实现了Iterable的接口,因此可以统一写成这样
validateVertex(v);
return adj[v];
}
public int degree(int v){
validateVertex(v);
return adj[v].size(); // Iterable没有size()方法
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append(String.format("V = %d, E = %d\n", V, E));
for (int i = 0; i < V; i++) {
sb.append(String.format("%d:",i));
for (int w: adj[i]) {
sb.append(String.format("%d ",w));
}
sb.append('\n');
}
return sb.toString();
}
public static void main(String[] args){
AdjSet adjSet = new AdjSet("g.txt"); //新建邻接矩阵,并从文件内容初始化
System.out.println(adjSet);
}
}