A mess of Graph Theory 1

The basic concept map

FIG assembly is composed of vertices and edges, G = <V, E>;

V is the set of points

E is the edge set

Directed edge, digraph

Undirected graph to the side, without

No right, point right to right side, a negative right

Ring, loopback, multiple edges, directed acyclic graph (DAG)

Path, the path is simple: the point has not been repeated, the communication

Tree: n-1 n-points of edges connected graph, complete graph: between any two points has an edge (undirected graph), tournament: the complete graph on each side a predetermined direction, tree ring group: there is a ring, the other full tree, cactus: there may be a ring, but each side, at most only present in one ring

Input mode map

The most common way is to use input 1 - N of vertices, edges and points given row and right edges size M is connected.
The NM
U1 W1 V1
U2 V2 w2 of
...
UM VM WM
if a little right, typically the size of a value indicating the weight of each point by the number of row N.
p1 p2... pn


It is the adjacency matrix storage easiest FIG.
FIG point for N, with a two-dimensional array of N × N recording if there is connected between the two sides, and the size of the right side.

Space complexity O (N ^ 2)
For the case where there is no side may use ∞, also possible to use a special value 0 or -1, etc., as the case may be.
In the case there are multiple edges, it may not be intact information all sides.


Adjacency list is more common storage FIG.
For each point u indefinite its starting shape <u, v> all sides, such as length of the array or linked list storage.
No split view of the bidirectional two-way side edges.

Variable-length arrays
List

Space complexity O (N + M)

FIG traversal methods:

BFS bfs queue

Dfs stack depth-first search


BFS breadth-first traversal
implemented queue according to a certain order of access to each node.

Creating a queue that contains the starting point
Remove the first team nodes, the adjacent nodes are not enqueued to queue
Repeat the previous step until all nodes have been traversed

For single-source shortest path problem on the map you have no right

DFS depth-first traversal
achieved with a stack (recursive functions), in accordance with a certain order for each access node.

Traversing from the starting point s
When traversing to u, recursively traverse all adjacent nodes have not been accessed v
When traversing s exit function, all the nodes are traversed

BFS and DFS traversal methods are the most basic of FIG more basic traversal methods are based on both,

BFS and DFS may be used to determine FIG communication request or communication sub FIG.

Both have advantages and disadvantages, to be selected according to actual situation

Combined with knowledge search

Three kinds of DFS order

Preorder traversal

Preorder

Subsequent traversal

Some simple examples

Example 1

1. Given a directed graph, the right side is 1 or 2, single-source shortest seek.

(Not used floyd, dijkstar, spfa etc.)

Open three queues, all on the first distance is 0 in the queue, and then enumerate all points a temporary point, if a queue, throw distance of 1, if the throw distance is a distance 2 2 queue

Create a set of three, Q0 represents the current layer, layer 1 represents Ql distance, Q2 represents a distance layer 2, the initial Q0 = {s}, Q1 = ∅, Q2 = ∅

Sequentially extracts the point Q0, which is placed corresponding to neighboring points in Q1 or Q2

And then Q0 = Q1, Q1 = Q2, Q2 = ∅

Note that a point may be both the current layer and the side length of 1, 2 have a length of the edge, it should not be added Q1 Q2.

Example 2

Given a directed graph and the starting point s, for each edge <u, v> answer omitted if this edge, from s to v shortest path length will change. Powerless;

Bfs seek again from the starting point s to the single-source shortest d [i] for each point, for an edge <u, v>, if d [v] = d [u] +1, described is the shortest side of the road ,Keep it;

A point of penetration of> = 2, it will not change or will change

Topological sorting

Directed acyclic graph topological sort is about to discharge all the vertices of a linear sequence, such that for any <u, v> ∈ E, u has a linear sequence occurs before v.
If there is a ring, then a topological sort must not exist in the figure; cycloalkyl If not, then there is the topological sort.


Select a point referred to as the degree of u 0

Adding to the linear sequence end u

Out side by deleting all of u

Repeat the above steps until all the points are added to the sequence

Example 1

There are n tasks, there are m constraints, limits the i th ui tasks must be completed before the task vi. I ask whether there is an appropriate task execution order to meet all the restrictions.

Each task as a point, dependencies between tasks constitute the directed edge. If there is no loop to the drawing, there topological sorting, the topological sort is feasible task execution sequence; if the presence of a ring to the drawing, no solution.

Example 2

There are n tasks, there are m limitation, the following two limitations:

V tasks must be completed before performing the task u

There is a time, u and v are in the task execution

I ask whether there is a schedule for each task start and end times of solutions to meet all of the restrictions

For each segment, represented by two points. In fact, it represents a time, one for the end of time;

For Task 1, that is, to the point of even starting time u side to the end time point of v;

After the task for 2, u start time to time after the end of v, v start time should be in the end time of u;

Then for each segment, but also the end point of the segment to start from this point and even an edge;

Example 3

For the right band edge directed acyclic graph, single-source shortest seek.

Referred to the starting point s, shaped like a topological sort $ a_1, a_2,..., A_t, s, b_1, b_2,..., B_k $, shortest note point i to di.

\(d[v]=\min\limits_{<u,v>∈E}\{d[u]+w_{u,v}\}\)

S departure can not come from \ (A_1, A_2,..., A_t \) , which is the shortest of these points is ∞

从 s 出发走到 bi 之前一定只经过 \(b_1, b_2, . . . , b_{i-1}\),即 dbi 的求解依赖于 \(d_{b_1}, d_{b_2}, . . . , d_{b_{i-1}}\)

按顺序依次求解 ds, db1, db2, . . . , dbk 即可

POJ 1094 Sorting It All Out

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B,C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specifed or not.
由一些不同元素组成的升序序列是可以用若干个小于号将所有的元素按从小到大的顺序 排列起来的序列。例如,排序后的序列为 A, B, C, D,这意味着 A < B、 B < C 和 C < D。在本题中,给定一组形如 A < B 的关系式,你的任务是判定是否存在一个有序序列。输出到哪一项可以确定顺序或者在这一项最先出现冲突,若所有的小于关系都处理完了都不能确定顺序也没有出现冲突,就输出不能确定。

冲突:拓扑排序排不出来就是冲突的(有环);

拓扑排序时,队列中永远没有出现大于一个元素的时候,就是唯一确定的;

不确定时,即队列中有时有>1个点;

BZOJ 2200 道路和航线

FJ 正在一个新的销售区域对他的牛奶销售方案进行调查。他想把牛奶送到 T 个城镇 (1 ≤ T ≤ 25000),编号为 1 到 T。这些城镇之间通过 R 条道路 (1 ≤ R ≤ 50000) 和 P 条航线(1 ≤ P ≤ 50000) 连接。每条道路 i 或者航线 i 连接城镇 Ai 到Bi,花费为 Ci。对于道路, 0 ≤ Ci ≤ 10000; 然而航线的花费很神奇,花费 Ci 可能是负数 (-10000 ≤ Ci ≤ 10000)。道路是双向的,可以从 Ai 到 Bi,也可以从 Bi 到 Ai,花费都是 Ci。然而航线与之不同,只可以从 Ai 到 Bi。事实上,由于最近恐怖主义太嚣张,为了社会和谐,出台了一些政策保证:如果有一条航线可以从 Ai 到 Bi,那么保证不可能通过一些道路和航线从 Bi 回到Ai。(有向边不存在于任何一个环,环都是正权无向边)由于 FJ 的奶牛世界公认〸分给力,他需要运送奶牛到每一个城镇。他想找到从发送中心城镇 S 把奶牛送到每个城镇的最便宜的方案,或者知道这是不可能的。 (求从s出发到某个点的单源最短路);O(nlogn)

把所有无向边加进去,形成若干连通块,看成一个大的点,然后加入有向边,形成一个DAG,如果有连通块的入度为0,直接连通块内跑dijkstar,有向边按拓扑序dp,更新dis,连通块内部使用dijkstar来做;


注意到如果有一条航线可以从 Ai 到 Bi,那么保证不可能通过一些道路和航线从 Bi 回到 Ai。
换言之,不存在包含航线(负权单向边)的环。
首先加入所有正权无向边,找出所有连通块,每个连通块缩为一个点。再加入所有单向边,此时图一定为 DAG。
在 DAG 上用 BFS 更新最短路,在正权无向边组成的连通块(缩点)内部使用 Dijkstra 更新最短路。



最短路

全局最短路 Floyd

单源最短路 Dijkstra SPFA Bellman-Ford

Floyd

基于动态规划, Fk,u,v, 表示使用点 1, 2, . . . , k 时,点 u 到点v 的最短路

从小到大枚举 k, u 和 v 之间的最短路要么不经过 k,要么经过 k 一次且除此之外只包含前 k - 1 个点

\(F_{k,u,v} =min\{F_{k-1,u,v}, F_{k-1,u,k} + F_{k-1,k,v}\}\)

for(int k=1;k<=n;k++)//k要写外面!
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++){
            f[k][i][j]=min(f[k][i][j],f[k-1][i][k]+f[k-1][k][j]);
            f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
        }

易见,使用二维数组不断覆盖更新即可。
时间复杂度 \(O(N^3)\)
空间复杂度 \(O(N^2)\)
可以处理含有负权边的情况,如果含有负环,则存在 i 使得
\(F_{i,i} < 0\)

传递闭包?

判断两点是否可达:

\(bool \ f[i][j]\)表示i到j是否可达:

for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            f[i][j]|=f[i][k]&f[k][j];

预防毒瘤出题人:

for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)if(f[i][k])
        for(int j=1;j<=n;j++)if(f[k][j])
            f[i][j]=true;

Dijkstar

适用于没有负权边的图。

将所有点分为两个集合,最短路确定的集合 S 和最短路未确定的集合 T,初始 S ={s}

求 T 中每个点 v 的当前最短路

\(d_v=\min_{<u,v>∈E,u∈S}\{d_u + w_{u,v}\}\)

取出 T 中 \(d_v\) 最小的点,其最短路一定就是 \(d_v\),将其加入 S

不断重复上面的操作,直到所有点的最短路都确定

朴素写法时间复杂度较劣,可以采用堆优化至$ O((N + M)logN) $

using pii=pair<int,int>

priority_queue<pii,veector<pii>,greater<pii> > q;

void dijkstar(){
    memset(dis,inf,sizeof(dis));
    q.push(make_pair(dis[s]=0,s));
    while(!q.empty()){
        int u=q.top().second;
        int d=q.top().first;
        q.pop();
        if(d!=dis[u]) continue;
        for(int i=head[u],v,w;i;i=edge[i].next){
            if(v=to[i],w=edge[i].dis,dis[v]>d+w)
                q.push(make_pair(dis[v]=d+w,v));
        }
    }
}

Bellman-Ford

初始令 \(d_s\) = 0,其余 \(d_i\) = ∞

依次使用每一条边 < u, v > 更新,$ d_v = min{d_v, d_u + w_{u,v}}$

不断循环直到所有 \(d_i\) 都不会更新

因为任何一条最短路包含至多 n - 1 条边,没有负环则不超过 n 轮就会停止

时间复杂度 O(NM)

空间复杂度 O(N + M)

可以对包含负权边的图使用,如果存在负环,则循环超过 n 轮后依然不会停止,可以用来判断负环是否存在。

SPFA

考虑使用队列优化 Bellman-Ford 算法,如果更新了 \(d_u\),则将 u入队。每次取队首 u 更新其邻点 v 的 \(d_v\)

最差复杂度O(NM);

Guess you like

Origin www.cnblogs.com/zhuier-xquan/p/11328917.html