0

I have a list of vertices and faces which index the vertex:

For example the list of vertices are (x,y,z) :

0 1 0
1 1 1
2 0 1
3 0 0
1 1 2
...
...
...

Faces that index the vertex:

0 1 2
2 3 4
4 0 1
... 
...
...

With the list of faces I need to group them up for those that faces are connected to each other. For the first 3 faces I know that they are connected to each other as they have common index. How do I implement an algorithm to do this? thanks.

I use the method from this link using depth first search to visit all the node but gets a segmentation fault inside the class

https://www.geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph/

#include <bits/stdc++.h>
using namespace std;
 
class Graph {
 
    // A function used by DFS
    void DFSUtil(int v);
 
public:
    int count;
    map<int, bool> visited;
    map<int, list<int> > adj;
    // function to add an edge to graph
    void addEdge(int v, int w);
 
    // prints DFS traversal of the complete graph
    void DFS();
};
 
void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w); // Add w to v’s list.
}
 
void Graph::DFSUtil(int v)
{
    // Mark the current node as visited and print it
    visited[v] = true;
    cout << v << " ";
 
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for (i = adj[v].begin(); i != adj[v].end(); ++i)
        if (!visited[*i])
            DFSUtil(*i);
}
 
// The function to do DFS traversal. It uses recursive
// DFSUtil()
void Graph::DFS()
{   
    count = 0;
    // Call the recursive helper function to print DFS
    // traversal starting from all vertices one by one
    for (auto i : adj)
        if (visited[i.first] == false)
         {
            DFSUtil(i.first);
            count++;
         }
}
 
int main()
{
    // Create a graph given in the above diagram
    Graph g;
    for face in faces :
    {
       g.addEdge(face[0], face[1]);
       g.addEdge(face[0], face[2]);
       g.addEdge(face[1], face[0]);
       g.addEdge(face[1], face[2]);
       g.addEdge(face[2], face[0]);
       g.addEdge(face[2], face[1]);
    }
    
 
    cout << "Following is Depth First Traversal \n";
       
      // Function call
    g.DFS();
    cout << "number of connected components = " << g.count << "\n";
    return 0;
}
Dong
  • 23
  • 5

0 Answers0