2
现在我的程序需要一个输入并给出一个单一的行输出。这个程序基本上使用了一个名为network-x的手动安装的python库(创建和分析图形,并且有许多内置的图形理论相关函数)。一次使用多个输入
对于这个特定的节目,它需要输入如下所示:
6 <-- Order of the graph (i.e the graph has 0 to 5 vertices)
1 2 3 <-- vertices going out from 0
2 <-- vertices going out from 1
3 5 <-- vertices going out from 2
4 <-- vertices going out from 3
5 <-- vertices going out from 4
<-- vertices going out from 5 (no vertices going out from 5)
和返回输出(即规定的图形连接了多少组件具有):
1
又如
6
3 5
2
4
4
和输出:输入的
2
我的程序的源代码是:
import networkx as nx
import sys
def main():
dag = nx.Graph()
order = int(input())
for i in range (order):
dag.add_node(i)
for j in range(order):
seq = [input()]
for vertex in seq:
vertexList = vertex.split()
for element in vertexList:
dag.add_edge(j,int(element))
j+=1
components = nx.number_connected_components(dag)
print (components)
main()
我目前停留在盘算如何让程序采取这两个输入一次,并计算2线路输出(每行输出结果每个输入),如下所示:在循环终止
6
1 2 3
2
3 5
4
5
6
3 5
2
4
4
0 <-- when the the user inputs a zero, the loop terminates
后它输出:
1
2
任何帮助,将不胜感激。 PSIF要安装网络-X库访问网络-X的GitHub的网站:https://networkx.github.io/documentation/latest/install.html
将'main'函数放入循环中,将'components'存储在列表中,'order'等于'0'时退出,并输出'components'列表。 – Holt