SPF

题目描述:

Consider the two networks shown below. Assuming that data moves around these networks only between directly connected nodes on a peer-to-peer basis, a failure of a single node, 3, in the network on the left would prevent some of the still available nodes from communicating with each other. Nodes 1 and 2 could still communicate with each other as could nodes 4 and 5, but communication between any other pairs of nodes would no longer be possible.

在这里插入图片描述
Node 3 is therefore a Single Point of Failure (SPF) for this network. Strictly, an SPF will be defined as any node that, if unavailable, would prevent at least one pair of available nodes from being able to communicate on what was previously a fully connected network. Note that the network on the right has no such node; there is no SPF in the network. At least two machines must fail before there are any pairs of available nodes which cannot communicate.

题意是说给你一个联通网路,求出这个网络所有割点的编号,以及如果删除这个割点之后所对应的联通分量数。

输入:

The input will contain the description of several networks. A network description will consist of pairs of integers, one pair per line, that identify connected nodes. Ordering of the pairs is irrelevant; 1 2 and 2 1 specify the same connection. All node numbers will range from 1 to 1000. A line containing a single zero ends the list of connected nodes. An empty network description flags the end of the input. Blank lines in the input file should be ignored.

输出:

For each network in the input, you will output its number in the file, followed by a list of any SPF nodes that exist.

The first network in the file should be identified as “Network #1”, the second as “Network #2”, etc. For each SPF node, output a line, formatted as shown in the examples below, that identifies the node and the number of fully connected subnets that remain when that node fails. If the network has no SPF nodes, simply output the text “No SPF nodes” instead of a list of SPF nodes.

样例输入:

1 2
5 4
3 1
3 2
3 4
3 5
0

1 2
2 3
3 4
4 5
5 1
0

1 2
2 3
3 4
4 6
6 3
2 5
5 1
0

0

样例输出:

Network #1
  SPF node 3 leaves 2 subnets

Network #2
  No SPF nodes

Network #3
  SPF node 2 leaves 2 subnets
  SPF node 3 leaves 2 subnets

code:

首先要明白什么是割点,什么是连通分量。离散数学的知识。
1、【割点】在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成多个连通块,就称这个点集为割点集合。当割点集合的顶点个数只有1个时,该顶点就是割点。
2、【连通分量】当删除某个割点后,原图会被划分为若干个互不连通的子图,这些子图就是该割点对应的连通分量。

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn=1007;
int head[maxn];
int low[maxn],dfn[maxn],visit[maxn];
int cut[maxn];
int n,cnt,num,count;
struct node{
	int to,next;
}edge[maxn*6];
void init()
{
	memset(dfn,0,sizeof(dfn));
	memset(head,-1,sizeof(head));
	memset(visit,0,sizeof(visit));
	memset(low,0,sizeof(low));
	memset(cut,0,sizeof(cut));
	cnt=0;
	num=0;
}
void add(int u,int v)
{
	edge[count].to=v;
	edge[count].next=head[u];
	head[u]=count++;
	edge[count].to=u;
	edge[count].next=head[v];
	head[v]=count++;
}
void tarjan(int what,int father)
{
	int child=0;
	visit[what]=1;
	low[what]=dfn[what]=++cnt;
	for(int i=head[what];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(visit[v]==1)
		{
			low[what]=min(low[what],dfn[v]);
		}
		if(visit[v]==0)
		{
			tarjan(v,what);
			child++;
			low[what]=min(low[what],low[v]);
			if((father==-1&&child>1)||(father!=-1&&low[v]>=dfn[what]))
			cut[what]++;
		}
	}
	visit[what]=2;
}
int main()
{
	init();
	int u,v;
	int cas=0,flag=0;
	while(~scanf("%d",&u))
	{
		if(u==0&&flag==1)
		{
			cas++;
			for(int i=1;i<=1000;i++)
			{
				if(visit[i]==0)
				{
					cnt=0;
					tarjan(i,-1);
				}
			}
			printf("Network #%d\n",cas);
			int f=0;
			for(int i=1;i<=1000;i++)
			{
				if(cut[i])
				{
					f=1;
					printf("  SPF node %d leaves %d subnets\n",i,cut[i]+1);
				}
			}
			if(f==0) printf("  No SPF nodes\n");
			printf("\n");
			init();
			flag=0;
		}
		else
		{
			flag=1;
			scanf("%d",&v);
			add(u,v);
		}
	}
	return 0;
}

                
        
### SPF算法的原理与实现 SPF(Shortest Path First)算法是一种基于图论的最短路径优先算法,由Dijkstra提出并广泛应用于计算机网络中的路由选择。它通过计算网络拓扑结构中节点之间的最短路径,为数据包转发提供最优路径[^1]。 #### 1. SPF算法的核心思想 SPF算法的核心是基于图论模型,将网络视为一个无向加权图 \( G(V, E) \),其中: - \( V \) 表示图中的顶点集合,通常代表路由器。 - \( E \) 表示图中的边集合,通常代表路由器之间的链路,每条边具有一个权重值,表示链路的开销或距离。 算法的目标是从源节点出发,找到到达所有其他节点的最短路径。在链路状态路由协议(如OSPF)中,每个路由器都会维护整个网络的拓扑信息,并根据这些信息构建自己的路由表[^2]。 #### 2. SPF算法的实现步骤 以下是SPF算法的一种典型实现方式: ```python import heapq def dijkstra(graph, start): # 初始化距离字典和优先队列 distances = {node: float('infinity') for node in graph} distances[start] = 0 priority_queue = [(0, start)] while priority_queue: current_distance, current_node = heapq.heappop(priority_queue) # 如果当前距离大于已知最小距离,则跳过 if current_distance > distances[current_node]: continue # 遍历邻居节点 for neighbor, weight in graph[current_node].items(): distance = current_distance + weight # 如果发现更短路径,则更新距离 if distance < distances[neighbor]: distances[neighbor] = distance heapq.heappush(priority_queue, (distance, neighbor)) return distances ``` 这段代码实现了Dijkstra算法的基本逻辑,适用于SPF算法的计算。`graph` 是一个字典形式的邻接表,表示网络的拓扑结构,`start` 是源节点。算法通过优先队列逐步扩展最短路径,直到遍历完所有可达节点[^1]。 #### 3. 在路由协议中的应用 在OSPF(Open Shortest Path First)协议中,SPF算法被用来计算路由表。每个路由器会广播其链路状态信息,形成一个完整的网络拓扑图。随后,路由器运行SPF算法,生成从自身到所有其他路由器的最短路径树(SPT)。最终,路由器根据SPT构建路由表,指导数据包的转发。 #### 4. 算法复杂度分析 SPF算法的时间复杂度主要取决于网络规模。对于含有 \( n \) 个节点和 \( m \) 条边的图,使用优先队列优化的Dijkstra算法的时间复杂度为 \( O((n + m) \log n) \)。在网络规模较大时,这种复杂度仍然是可接受的[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值