Graph theory basic and applied

Basics

Representation of FIG.

FIG representation are adjacency matrix and adjacency lists

  1. Adjacency matrix: FIG suitable for dense (the number of edges is close to complete graph)
  2. Adjacency lists: for sparse graphs (far less than the number of edges in the complete graph)

Disjoint-set

Minimum spanning tree

Code Step

  1. Defined set of edges
  2. Disjoint-set part
  3. kruskal algorithm part: 1) Initialization 2 disjoint-set) in ascending order of the right side edge of sorts 3) traversing edge

Code

//图
#include <iostream>
#include <algorithm>

using namespace std;

const int MAXV = 1010;
const int MAXE = 1010;

//边集定义部分
struct edge {
	int u, v;
	int cost;
}E[MAXE];
bool cmp(edge a, edge b) {
		return a.cost < b.cost;
}

//并查集部分
int Tree[MAXV];
int findRoot(int x)
{
	if (Tree[x] == -1) return x;
	else
	{
		int temp = findRoot(Tree[x]);
		Tree[x] = temp;
		return temp;
	}
}

//kruskal部分
int kruskal(int n, int m)
{
	//n:顶点数,m:边数
	int ans = 0; int Num_Edge = 0;
	for (int i = 1; i <= n; i++)
		Tree[i] = -1;
	sort(E + 1, E + m + 1, cmp);
	for (int i = 1; i <= m; i++)
	{
		int a = findRoot(E[i].u);
		int b = findRoot(E[i].v);
		if (a != b)
		{
			Tree[a] = b;
			ans = ans + E[i].cost;
			Num_Edge++;
		}
		if (Num_Edge == n - 1) break;
	}
	if (Num_Edge == n - 1) return ans;
	else return -1;
}


int main()
{
	int n;
	while (scanf("%d", &n) != EOF && n != 0)
	{
		int m = n * (n - 1) / 2;
		for (int i = 1; i <= m; i++)
			scanf("%d %d %d", &E[i].u, &E[i].v, &E[i].cost);
		cout << kruskal(n, m) << endl;

	}

	system("pause");
	return 0;


}

Shortest Path - dijkstra algorithm

Solving the communicating FIG single-source shortest path , the shortest path to give the starting point s to other points.
Second scale: when there are multiple shortest paths, as a second scale to measure, for example: the right side (minimum cost), right point (maximum demand) number of the shortest distance

Code Step

1. Initialization

  1. The shortest distance
    d [u]: d [s ] = 0 other d [u] = INF
  2. Right side
    c [u]: c [s ] = 0 other c [u] = INF
  3. Point right
    w [u]: w [s ] = weight [s] other w [u] = 0
  4. The number of the shortest distance
    num [u]: num [s ] = 1 else num [u] = 0

(N times the cycle of steps until all nodes visited n)
2 is not accessible in the set, to find that the D [u] u minimum node
3. u access node, i.e. the node has been put u visited set S
4. optimization d [v]: u as to update all the intermediate nodes d [v]

Code

//开始玩转Dijkstra
//Dijkstra模板
//模板思路:1)在未访问的点的集合中寻找使d[u]最小的u	2)在未访问的结点中,更新所有以u为中间结点的d[v]

#include <iostream>

using namespace std;

const int MAXV = 1100;
const int INF = 1e9;

int G[MAXV][MAXV];  //存放图,以邻接矩阵的形式
int d[MAXV];		//当前最短路径:起点到达各个终点的最短路径长度  d[u]:源结点s到结点u的最短路径
bool visit[MAXV] = {false};	//用来判断当前节点是否已经被访问

void dijkstra(int n, int s)
{
	//n为顶点数,s为起始节点

	//数组d[MAXV]的初始化
	fill(d + 1, d + n + 1, INF);
	d[s] = 0;

	for (int i = 1; i <= n; i++)
	{
		//寻找最小的d[u]的标号u
		int temp = INF; int u;
		for (int j = 1; j <= n; j++)
		{
			if (visit[j] == false && d[j] < temp)
			{
				temp = d[j];
				u = j;
			}
		}
		visit[u] = true;
		for (int v = 1; v <= n; v++)
		{
			if (visit[v] == false && G[u][v] != INF && G[u][v] + d[u] < d[v])
				d[v] = G[u][v] + d[u];
		}
	}


}

int main()
{
	int n, m,s;
	int u, v, cost;
	scanf("%d %d %d", &n, &m, &s);
	fill(G[0], G[0] + MAXV * MAXV, INF);
	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d %d", &u, &v, &cost);
		G[u][v] = cost;
	}
	dijkstra(n, s);
	for (int i = 1; i <= n; i++)
		cout << d[i] << " ";
	
	system("pause");
	return 0;
}
Released nine original articles · won praise 4 · Views 1018

Guess you like

Origin blog.csdn.net/L18273121172/article/details/90513866