给定一个由V顶点和E边,源顶点src和目标顶点dest组成的连通无环图,任务是计算图中给定源顶点和目标顶点之间的顶点数量。
例子:
Input: V = 8, E = 7, src = 7, dest = 8, edges[][] ={{1 4}, {4, 5}, {4, 2}, {2, 6}, {6, 3}, {2, 7}, {3, 8}}
Output: 3
Explanation:
The path between 7 and 8 is 7 -> 2 -> 6 -> 3 -> 8.
So, the number of nodes between 7 and 8 is 3.
Input: V = 8, E = 7, src = 5, dest = 2, edges[][] ={{1 4}, {4, 5}, {4, 2}, {2, 6}, {6, 3}, {2, 7}, {3, 8}}
Output: 3
Explanation:
The path between 5 and 2 is 5 -> 4 -> 2.
So, the number of nodes between 5 and 2 is 1.
方法:也可以使用本文所述的“不相交联合”方法解决该问题。解决此问题的另一种方法是使用“深度优先搜索”方法解决。请按照以下步骤解决此问题:
- 初始化访问数组vis []以标记已访问过的节点。将所有节点标记为0,即未访问。
- 执行DFS查找src和dest之间的路径中存在的节点数。
- src和dest之间的节点数等于它们和2之间的路径长度之差,即(pathSrcToDest – 2 )。
- 由于该图是非循环的且已连接,因此src和dest之间始终只有一条路径。
下面是上述算法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the count of nodes
// in the path from source to destination
int dfs(int src, int dest, int* vis,
vector* adj)
{
// Mark the node visited
vis[src] = 1;
// If dest is reached
if (src == dest) {
return 1;
}
// Traverse all adjacent nodes
for (int u : adj[src]) {
// If not already visited
if (!vis[u]) {
int temp = dfs(u, dest, vis, adj);
// If there is path, then
// include the current node
if (temp != 0) {
return temp + 1;
}
}
}
// Return 0 if there is no path
// between src and dest through
// the current node
return 0;
}
// Function to return the
// count of nodes between two
// given vertices of the acyclic Graph
int countNodes(int V, int E, int src, int dest,
int edges[][2])
{
// Initialize an adjacency list
vector adj[V + 1];
// Populate the edges in the list
for (int i = 0; i < E; i++) {
adj[edges[i][0]].push_back(edges[i][1]);
adj[edges[i][1]].push_back(edges[i][0]);
}
// Mark all the nodes as not visited
int vis[V + 1] = { 0 };
// Count nodes in the path from src to dest
int count = dfs(src, dest, vis, adj);
// Return the nodes between src and dest
return count - 2;
}
// Driver Code
int main()
{
// Given number of vertices and edges
int V = 8, E = 7;
// Given source and destination vertices
int src = 5, dest = 2;
// Given edges
int edges[][2]
= { { 1, 4 }, { 4, 5 },
{ 4, 2 }, { 2, 6 },
{ 6, 3 }, { 2, 7 },
{ 3, 8 } };
cout << countNodes(V, E, src, dest, edges);
return 0;
}
Java
// Java program for the above approach
import java.util.Vector;
class GFG{
// Function to return the count of nodes
// in the path from source to destination
static int dfs(int src, int dest, int []vis,
Vector []adj)
{
// Mark the node visited
vis[src] = 1;
// If dest is reached
if (src == dest)
{
return 1;
}
// Traverse all adjacent nodes
for (int u : adj[src])
{
// If not already visited
if (vis[u] == 0)
{
int temp = dfs(u, dest,
vis, adj);
// If there is path, then
// include the current node
if (temp != 0)
{
return temp + 1;
}
}
}
// Return 0 if there is no path
// between src and dest through
// the current node
return 0;
}
// Function to return the
// count of nodes between two
// given vertices of the acyclic Graph
static int countNodes(int V, int E,
int src, int dest,
int edges[][])
{
// Initialize an adjacency list
Vector []adj = new Vector[V + 1];
for (int i = 0; i < adj.length; i++)
adj[i] = new Vector();
// Populate the edges in the list
for (int i = 0; i < E; i++)
{
adj[edges[i][0]].add(edges[i][1]);
adj[edges[i][1]].add(edges[i][0]);
}
// Mark all the nodes as
// not visited
int vis[] = new int[V + 1];
// Count nodes in the path
// from src to dest
int count = dfs(src, dest,
vis, adj);
// Return the nodes
// between src and dest
return count - 2;
}
// Driver Code
public static void main(String[] args)
{
// Given number of vertices and edges
int V = 8, E = 7;
// Given source and destination vertices
int src = 5, dest = 2;
// Given edges
int edges[][] = {{1, 4}, {4, 5},
{4, 2}, {2, 6},
{6, 3}, {2, 7},
{3, 8}};
System.out.print(countNodes(V, E,
src, dest,
edges));
}
}
// This code is contributed by shikhasingrajput
Python3
# Python3 program for the above approach
# Function to return the count of nodes
# in the path from source to destination
def dfs(src, dest, vis, adj):
# Mark the node visited
vis[src] = 1
# If dest is reached
if (src == dest):
return 1
# Traverse all adjacent nodes
for u in adj[src]:
# If not already visited
if not vis[u]:
temp = dfs(u, dest, vis, adj)
# If there is path, then
# include the current node
if (temp != 0):
return temp + 1
# Return 0 if there is no path
# between src and dest through
# the current node
return 0
# Function to return the
# count of nodes between two
# given vertices of the acyclic Graph
def countNodes(V, E, src, dest, edges):
# Initialize an adjacency list
adj = [[] for i in range(V + 1)]
# Populate the edges in the list
for i in range(E):
adj[edges[i][0]].append(edges[i][1])
adj[edges[i][1]].append(edges[i][0])
# Mark all the nodes as not visited
vis = [0] * (V + 1)
# Count nodes in the path from src to dest
count = dfs(src, dest, vis, adj)
# Return the nodes between src and dest
return count - 2
# Driver Code
if __name__ == '__main__':
# Given number of vertices and edges
V = 8
E = 7
# Given source and destination vertices
src = 5
dest = 2
# Given edges
edges = [ [ 1, 4 ], [ 4, 5 ],
[ 4, 2 ], [ 2, 6 ],
[ 6, 3 ], [ 2, 7 ],
[ 3, 8 ] ]
print(countNodes(V, E, src, dest, edges))
# This code is contributed by mohit kumar 29
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the count of nodes
// in the path from source to destination
static int dfs(int src, int dest,
int []vis, List []adj)
{
// Mark the node visited
vis[src] = 1;
// If dest is reached
if (src == dest)
{
return 1;
}
// Traverse all adjacent nodes
foreach (int u in adj[src])
{
// If not already visited
if (vis[u] == 0)
{
int temp = dfs(u, dest,
vis, adj);
// If there is path, then
// include the current node
if (temp != 0)
{
return temp + 1;
}
}
}
// Return 0 if there is no path
// between src and dest through
// the current node
return 0;
}
// Function to return the
// count of nodes between two
// given vertices of the acyclic Graph
static int countNodes(int V, int E,
int src, int dest,
int [,]edges)
{
// Initialize an adjacency list
List []adj = new List[V + 1];
for (int i = 0; i < adj.Length; i++)
adj[i] = new List();
// Populate the edges in the list
for (int i = 0; i < E; i++)
{
adj[edges[i, 0]].Add(edges[i, 1]);
adj[edges[i, 1]].Add(edges[i, 0]);
}
// Mark all the nodes as
// not visited
int []vis = new int[V + 1];
// Count nodes in the path
// from src to dest
int count = dfs(src, dest,
vis, adj);
// Return the nodes
// between src and dest
return count - 2;
}
// Driver Code
public static void Main(String[] args)
{
// Given number of vertices and edges
int V = 8, E = 7;
// Given source and destination vertices
int src = 5, dest = 2;
// Given edges
int [,]edges = {{1, 4}, {4, 5},
{4, 2}, {2, 6},
{6, 3}, {2, 7},
{3, 8}};
Console.Write(countNodes(V, E, src,
dest, edges));
}
}
// This code is contributed by 29AjayKumar
输出:
1
时间复杂度: O(V + E)
辅助空间: O(V)