1. 图的分类
2. 图的基本概念(联通分量、联通图的生成树)
3. 图的基本表示(无向无权图为例)
实现代码:
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;
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");
E = scanner.nextInt();
if (E < 0) throw new IllegalArgumentException("E must be non-negative");
adj = new int[V][V];
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 is Detected");
adj[a][b] = 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;
}
//返回顶点a的所有邻接点
public ArrayList<Integer> adj(int v){
validateVertex(v);
ArrayList<Integer> ret = new ArrayList<Integer>();
for (int i = 0; i < V; i++) {
if(adj[v][i] == 1){
ret.add(i);
}
}
return ret;
}
//返回顶点a的度
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++) {
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);
}
}
实现方式一:链表存储
import javax.sound.sampled.Line;
import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
public class AdjList {
private int V;
private int E;
private LinkedList<Integer>[] adj;
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");
E = scanner.nextInt();
if (E < 0) throw new IllegalArgumentException("E must be non-negative");
adj = new LinkedList[V];
for (int i = 0; i < V; i++) {
adj[i] = new LinkedList<Integer>();
}
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");
//此处查找时间复杂度为 O(V)
if (adj[a].contains(b)) throw new IllegalArgumentException("Parallel Edges is Detected");
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);
}
//返回顶点a的所有邻接点
public LinkedList<Integer> adj(int v) {
validateVertex(v);
return adj[v];
}
//返回顶点a的度
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 v = 0; v < V; v++) {
sb.append(String.format("%d: ", v));
for (int w : adj[v]) {
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);
}
}
实现方式二:红黑树存储
import com.sun.source.tree.Tree;
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;
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");
E = scanner.nextInt();
if (E < 0) throw new IllegalArgumentException("E must be non-negative");
adj = new TreeSet[V];
for (int i = 0; i < V; i++) {
adj[i] = new TreeSet<Integer>();
}
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");
//此处查找时间复杂度为 O(logV)
if (adj[a].contains(b)) throw new IllegalArgumentException("Parallel Edges is Detected");
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);
}
//返回顶点a的所有邻接点
public Iterable<Integer> adj(int v) {
validateVertex(v);
return adj[v];
}
//返回顶点a的度
public int degree(int v) {
validateVertex(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 v = 0; v < V; v++) {
sb.append(String.format("%d: ", v));
for (int w : adj[v]) {
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);
}
}
综合比较
- 注:本系列参考玩转算法系列–图论精讲