图的宽度优先遍历

读入一个用邻接矩阵存储的无向图,输出它的宽度优先遍历序列。 

输入

第 1 行 1 个正整数 n,表示图中顶点数,2≤n≤100; 

接下来的 n 行是一个 n×n 的邻接矩阵,a[i][j]=1 表示顶点 i 和顶点 j 之间有直接边相连,a[i][j]=0 表示没有直接边相连。保证 i=j 时,a[i][j]=0,并且 a[i][j]=a[j][i]。 

输出

输出 1~n 的某一种排列,表示从顶点 1 开始,对该图进行宽度优先遍历得到的顶点序列,每两个数之间用一个“-”分隔。

输入

8
0 1 1 0 0 0 0 0
1 0 0 1 1 0 0 0
1 0 0 0 0 0 1 1
0 1 0 0 0 1 0 0
0 1 0 0 0 1 0 0
0 0 0 1 1 0 0 0
0 0 1 0 0 0 0 1
0 0 1 0 0 0 1 0

输出

1-2-3-4-5-7-8-6

思路:

节点不重复遍历!为什么要管图连不连通呢?为什么不能一起考虑呢?

图所示为图的宽度优先遍历。 

用队列来实现图的宽度优先遍历: 

先从某个顶点出发,把这个顶点入队,作为队首元素。然后把跟队首元素相连的顶点再依次入队,最后队首元素出队。接着再把新的队首元素相连的所有顶点入队,新的队首元素出队。……如此下去,直到所有元素都出队,出队的顺序就是图的宽度优先遍历序列。

CODE:


                
以下是的深度优先遍历宽度优先遍历的介绍: 深度优先遍历(DFS): 深度优先遍历是一种用于遍历或搜索树或算法。在这种搜索方法中,从根结点开始,尽可能深地搜索每个分支,直到到达最深的结点,然后回溯到前一个结点,继续搜索另一个分支,直到所有结点都被访问为止。深度优先遍历使用栈来实现,因此也称为“栈搜索”。 以下是深度优先遍历的Python代码示例: ```python # Python3 program to print DFS traversal # from a given given graph from collections import defaultdict # This class represents a directed graph using # adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # A function used by DFS def DFSUtil(self, v, visited): # Mark the current node as visited # and print it visited.add(v) print(v, end=' ') # Recur for all the vertices # adjacent to this vertex for neighbour in self.graph[v]: if neighbour not in visited: self.DFSUtil(neighbour, visited) # The function to do DFS traversal. It uses # recursive DFSUtil() def DFS(self, v): # Create a set to store visited vertices visited = set() # Call the recursive helper function # to print DFS traversal self.DFSUtil(v, visited) ``` 宽度优先遍历BFS): 宽度优先遍历是一种用于遍历或搜索树或算法。在这种搜索方法中,从根结点开始,逐层遍历每个结点的所有子结点,直到到达最深的结点。宽度优先遍历使用队列来实现。 以下是宽度优先遍历的Python代码示例: ```python # Python3 program to print BFS traversal # from a given source vertex. BFS(int s) # traverses vertices reachable from s. from collections import defaultdict # This class represents a directed graph # using adjacency list representation class Graph: # Constructor def __init__(self): # default dictionary to store graph self.graph = defaultdict(list) # function to add an edge to graph def addEdge(self, u, v): self.graph[u].append(v) # Function to print a BFS of graph def BFS(self, s): # Mark all the vertices as not visited visited = [False] * (max(self.graph) + 1) # Create a queue for BFS queue = [] # Mark the source node as # visited and enqueue it queue.append(s) visited[s] = True while queue: # Dequeue a vertex from # queue and print it s = queue.pop(0) print(s, end=" ") # Get all adjacent vertices of the # dequeued vertex s. If a adjacent # has not been visited, then mark it # visited and enqueue it for i in self.graph[s]: if visited[i] == False: queue.append(i) visited[i] = True ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值