图的数据结构以及基础算法

邻接矩阵(C++实现)

定义

#define MAXV 10    //最大节点个数
#define INF 32767	//定义无穷大

typedef struct
{
	int no;
	int info;
}VertexType;//存放定点信息 
typedef struct
{
	int edges[MAXV][MAXV];//邻接数组
	int n, e;//顶点数,边数
	VertexType vexs[MAXV];//顶点数组
}MatGraph;//图邻接矩阵类型

基本操作

void CreatMat(MatGraph &g, int A[MAXV][MAXV], int n, int e){
	g.n = n; g.e = e;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
			g.edges[i][j] = A[i][j];
}

void DisplayMat(MatGraph G){
	cout << "邻接矩阵:" << endl;
	for(int i = 0; i < G.n; i++)
	{ 
		for (int j = 0; j < G.n; j++)
		{
			if (G.edges[i][j] == INF)
				cout << "∞   ";
			else
				cout << G.edges[i][j] << "   ";
		}
		cout << endl;
	}
	cout << endl;
}

邻接表(C++实现)

定义

typedef struct Anode
{
	int no;//该节点编号
	int weight;//该边的信息,如权值
	struct Anode *nextarc;//指向下一个节点的指针
}ArcNode;//邻接表边类型 即指向结点的边
typedef struct Vnode
{
	char info;//头结点信息  注意不是结点编号
	ArcNode *firstarc;//指向第一个邻接点的指针
}VNode;//邻接表头节点类型
typedef struct
{
	VNode adjlist[MAXV];//顶点数组  MAXV:最大结点数
	int n, e;//n:顶点数
}AdjGraph;//图邻接表类型

基本操作

创建

void CreatAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e){
	
	G = (AdjGraph *)malloc(sizeof(AdjGraph));
	G->n = n; G->e = e;
	for(int i = 0; i < n; ++i)
		G->adjlist[i].firstarc = NULL;
		
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
			if(A[i][j] != 0 && A[i][j] != INF){
				ArcNode *p = (ArcNode *)malloc(sizeof(ArcNode));
				p->no = j;//notice**
				p->weight = A[i][j];
				
				ArcNode *r = NULL;
				r = G->adjlist[i].firstarc;
				
				p->nextarc = G->adjlist[i].firstarc;//头插法,顺序和数组是反的 
				G->adjlist[i].firstarc = p;//这里用不了尾插,因为没有头结点 
			}
}

输出

void DisplayAdj(AdjGraph *G){
	cout << "adjacent graph: " << endl;
	for(int i = 0; i < G->n; ++i){
		cout << "node " << i << ": ";
		ArcNode *p = G->adjlist[i].firstarc;
		while(p != NULL){
			cout << p->no << "[weight: "<< p->weight <<"] ";
			p = p->nextarc;
		}
		cout << endl;
	}
	cout << endl;
}

销毁

void Destroy(AdjGraph *&G)
{
	ArcNode *q;
	for (int i = 0; i < G->n; i++)
	{
		ArcNode *p = G->adjlist[i].firstarc;
		
		while (p != NULL)
		{
			q = p;
			p = p->nextarc;
			free(q);
			q = NULL;
		}
	}
	free(G);
	G = NULL;
	cout << "Destroy adjacent Graph..." << endl;
}

调用代码

	MatGraph g;// Matrix Graph
	AdjGraph *G;//Adjacent Graph
	int A[MAXV][MAXV] = {
		{0,   5,   INF, 7,   INF, INF},
		{INF, 0,   4,   INF, INF, INF},
		{8,   INF, 0,   INF, INF, 9  },
		{INF, INF, 5,   0,   INF, 6  },
		{INF, INF, INF, 5,   0,   INF},
		{3,   INF, INF, INF, 1,   0  },
	};//directed graph
	int A2[MAXV][MAXV] = {
		{0, 1,   1,   1},
		{1, 0,   INF, 1},
		{1, INF, 0,   1},
		{1, 1,   1,   0}
	};//无向图
	int n = 4, e = 5;
	CreatMat(g, A2, n, e);
	//DisplayMat(g);
	CreatAdj(G, A2, n, e);
	//DisplayAdj(G);

邻接表完整代码(Java实现)

public class AdjGraph {
    
    
//	private static final int MAXV = 100;
	private static final int INF = 999999;
	static final int n = 4;
	static final int e = 5;
	
	static class Graph{
    
    
		ArrayList<Vnode> adjlist;
		int n, e;
		Graph(int n, int e, ArrayList<Vnode> adjlist){
    
    
			this.n = n;
			this.e = e;
			this.adjlist = adjlist;
		}
	}
	static class Vnode{
    
    
		char info;
		Arcnode firstarc;
	}
	static class Arcnode{
    
    
		int no;
		int weight;
		Arcnode nextarc;
		Arcnode(int weight) {
    
    this.weight = weight;}
	}

	public static void main(String[] args) {
    
    
		int[][] arr = {
    
    
			{
    
    0, 1,   1,   1},
			{
    
    1, 0,   INF, 1},
			{
    
    1, INF, 0,   1},
			{
    
    1, 1,   1,   0}
		};
		Graph g = createAdj(arr, n, e);
		dispAdj(g);
		System.out.println();
		dfs(g, 1);
	}
	static int vn = 0, en = 0;

	public static Graph createAdj(int[][] arr, int n, int e) {
    
    
		Graph g = new Graph(n, e, new ArrayList<Vnode>());
		for(int i = 0; i < n; i++) {
    
    
			g.adjlist.add(new Vnode());
		}
		for(int i = 0; i < n; i++) {
    
    
			for(int j = 0; j < n; j++) {
    
    
				if(arr[i][j] != INF && arr[i][j] != 0) {
    
    
					Arcnode arcnode = new Arcnode(arr[i][j]);//初始化边
					arcnode.no = j;//该边指向的结点编号
					//尾插法
					if(g.adjlist.get(i).firstarc == null)
						g.adjlist.get(i).firstarc = arcnode;
					else {
    
    
						Arcnode p = g.adjlist.get(i).firstarc;
						while(p.nextarc != null) {
    
    
							p = p.nextarc;
						}
						p.nextarc = arcnode;
					}
					//头插法,简单一点,只需2行
//					arcnode.nextarc = g.adjlist.get(i).firstarc;
//					g.adjlist.get(i).firstarc = arcnode;
				}
			}
		}
		return g;
	}
	public static void dispAdj(Graph g) {
    
    
		for(int i = 0; i < g.n; i++) {
    
    
			System.out.print("no:" + i + " :: ");
			Arcnode arc = g.adjlist.get(i).firstarc;
			while(arc != null) {
    
    
				System.out.print("no:" + arc.no + " --> ");
				arc = arc.nextarc;
			}
			System.out.println();
		}
	}
}

BFS及DFS实现(Java)

public static void bfs(Graph g, int v) {
    
    
    int[] visited = new int[g.n];
    int[] queue = new int[g.n];
    int front = 0, rear = 0;
    visited[v] = 1;
    System.out.print(v + " ");
    rear = (rear + 1) % g.n;
    queue[rear] = v;
    while(front != rear) {
    
    
        front = (front + 1) % g.n;
        int t = queue[front];
        Arcnode arc = g.adjlist.get(t).firstarc;
        while(arc != null) {
    
    
            if(visited[arc.no] == 0) {
    
    
                visited[arc.no] = 1;
                System.out.print(arc.no + " ");
                rear = (rear + 1) % g.n;
                queue[rear] = arc.no;
            }
            arc = arc.nextarc;
        }
    }
}

static int[] dfs_visited = new int[n];

public static void dfs(Graph g, int v) {
    
    
    dfs_visited[v] = 1;
    System.out.print(v + " ");
    Arcnode a = g.adjlist.get(v).firstarc;
    while(a != null) {
    
    
        if(dfs_visited[a.no] == 0) {
    
    
            dfs(g, a.no);
        }
        else a = a.nextarc;
    }
}

猜你喜欢

转载自blog.csdn.net/rakish_wind/article/details/120792441