Клонировать неориентированный граф с несколькими связными компонентами
Учитывая неориентированный граф с несколькими связными компонентами, задача состоит в том, чтобы клонировать граф. Клонирование графа с одним компонентом связности можно увидеть здесь.
Примеры:
Пример неориентированного графа с 3 подключенными компонентами:

Подход:
 Идея состоит в том, чтобы следовать тому же подходу, что и для клонирования связного графа, но с каждым узлом, чтобы мы могли клонировать графы с несколькими связными компонентами.
Мы собираемся использовать класс GraphNode и класс Graph. Класс Graph является обязательным, поскольку у нас может быть несколько связанных компонентов (см. Пример выше), и мы не можем иметь дело с ними, имея только GraphNode в качестве входных данных. Для класса Graph нам действительно нужен список GraphNodes. Также возможно составить список узлов вместо создания класса, работают оба способа.
Чтобы отслеживать посещенные узлы, нам нужна структура данных; карта является подходящей, поскольку мы можем отображать «старые» узлы в «новые» (клонированные). Итак, мы определяем основную функцию, которая создает карту и использует вспомогательную функцию для ее заполнения. После создания карты можно создать новый граф, используя клонированные узлы на карте.
 Вспомогательная функция будет устанавливать связи между узлами (помимо заполнения карты). Поскольку мы имеем дело со всем подключенным компонентом, мы будем придерживаться аналогичного подхода к BFS.
Обратите внимание, что в основной функции мы не вызываем вспомогательную функцию для каждого узла в Graph; если узел хранится на карте, это означает, что мы уже посетили его и разобрались с его подключенным компонентом, поэтому нет необходимости повторять шаги снова.
 Чтобы проверить, правильно ли был клонирован граф, мы можем распечатать адреса памяти узлов и сравнить их, чтобы увидеть, клонировали ли мы их или скопировали.
Below is the implementation of the above approach:
C++14
| // C++ implementation of the approach#include <bits/stdc++.h>usingnamespacestd;// GraphNode class represents each// Node of the GraphclassGraphNode{    intdata;    list<GraphNode *> children;        // Constructor to initialize the    // node with value    public:        GraphNode(intdata)        {            this->data = data;        }                // Function to add a child to the        // current node        voidaddChild(GraphNode *node)        {            this->children.push_back(node);        }                // Function to return a list of children        // for the current node        list<GraphNode *> getChildren()        {            returnchildren;        }                // Function to set the node"s value        voidsetData(intdata)        {            this->data = data;        }                // Function to return the node"s value        intgetData()        {            returndata;        }};// Class to represent the graphclassGraph{    list<GraphNode *> nodes;        public:        Graph(){}                // Constructor to set the graph"s nodes        Graph(list<GraphNode *> nodes)        {            this->nodes = nodes;        }                // Function to add a node to the graph        voidaddNode(GraphNode *node)        {            this->nodes.push_back(node);        }                // Function to return the list of nodes        // for the graph        list<GraphNode *> getNodes()        {            returnthis->nodes;        }};classGFG{    // Function to clone the graph// Function to clone the connected componentsvoidcloneConnectedComponent(GraphNode *node,                             map<GraphNode *,                             GraphNode *> &map){    queue<GraphNode *> queue;    queue.push(node);        while(!queue.empty())    {        GraphNode *current = queue.front();        queue.pop();        GraphNode *currentCloned = NULL;                if(map.find(current) != map.end())        {            currentCloned = map[current];        }        else        {            currentCloned = newGraphNode(                current->getData());            map[current] = currentCloned;        }                list<GraphNode *> children = current->getChildren();        for(autochild : children)        {            if(map.find(child) != map.end())            {                currentCloned->addChild(map[child]);            }            else            {                GraphNode *childCloned = newGraphNode(                    child->getData());                map[child] = childCloned;                currentCloned->addChild(childCloned);                queue.push(child);            }        }    }}public:    Graph *cloneGraph(Graph *graph)    {        map<GraphNode *, GraphNode *> mapp;        for(autonode : graph->getNodes())        {            if(mapp.find(node) == mapp.end())                cloneConnectedComponent(node, mapp);        }        Graph *cloned = newGraph();        for(autocurrent : mapp)            cloned->addNode(current.second);                returncloned;    }// Function to build the graphGraph *buildGraph(){        // Create graph    Graph *g = newGraph();        // Adding nodes to the graph    GraphNode *g1 = newGraphNode(1);    g->addNode(g1);    GraphNode *g2 = newGraphNode(2);    g->addNode(g2);    GraphNode *g3 = newGraphNode(3);    g->addNode(g3);    GraphNode *g4 = newGraphNode(4);    g->addNode(g4);    GraphNode *g5 = newGraphNode(5);    g->addNode(g5);    GraphNode *g6 = newGraphNode(6);    g->addNode(g6);        // Adding edges    g1->addChild(g2);    g1->addChild(g3);    g2->addChild(g1);    g2->addChild(g4);    g3->addChild(g1);    g3->addChild(g4);    g4->addChild(g2);    g4->addChild(g3);    g5->addChild(g6);    g6->addChild(g5);        returng;}// Function to print the connected componentsvoidprintConnectedComponent(GraphNode *node,                         set<GraphNode *> &visited){    if(visited.find(node) != visited.end())        return;    queue<GraphNode *> q;    q.push(node);        while(!q.empty())    {        GraphNode *currentNode = q.front();        q.pop();                if(visited.find(currentNode) != visited.end())            continue;                    visited.insert(currentNode);        cout << "Node "<< currentNode->getData()             << " - "<< currentNode << endl;        for(GraphNode *child : currentNode->getChildren())        {            cout << "	Node "<< child->getData()                 << " - "<< child << endl;            q.push(child);        }    }}};// Driver codeintmain(){    GFG *gfg = newGFG();    Graph *g = gfg->buildGraph();        // Original graph    cout << "	INITIAL GRAPH
";    set<GraphNode *> visited;    for(GraphNode *n : g->getNodes())        gfg->printConnectedComponent(n, visited);        // Cloned graph    cout << "
	CLONED GRAPH
";    Graph *cloned = gfg->cloneGraph(g);    visited.clear();    for(GraphNode *node : cloned->getNodes())        gfg->printConnectedComponent(node, visited);}// This code is contributed by sanjeev2552 | 
Java
| // Java implementation of the approachimportjava.util.ArrayList;importjava.util.HashMap;importjava.util.HashSet;importjava.util.LinkedList;importjava.util.List;importjava.util.Map;importjava.util.Queue;importjava.util.Set;// Class to represent the graphclassGraph {    privateList<GraphNode> nodes;    // Constructor to create an empty ArrayList    // to store the nodes of the graph    publicGraph()    {        this.nodes = newArrayList<GraphNode>();    }    // Constructor to set the graph"s nodes    publicGraph(List<GraphNode> nodes)    {        this.nodes = nodes;        this.nodes = newArrayList<GraphNode>();    }    // Function to add a node to the graph    publicvoidaddNode(GraphNode node)    {        this.nodes.add(node);    }    // Function to return the list of nodes    // for the graph    publicList<GraphNode> getNodes()    {        returnthis.nodes;    }}// GraphNode class represents each// Node of the GraphclassGraphNode {    privateintdata;    privateList<GraphNode> children;    // Constructor to initialize the node with value    publicGraphNode(intdata)    {        this.data = data;        this.children = newArrayList<GraphNode>();    }    // Function to add a child to the current node    publicvoidaddChild(GraphNode node)    {        this.children.add(node);    }    // Function to return a list of children    // for the current node    publicList<GraphNode> getChildren()    {        returnchildren;    }    // Function to set the node"s value    publicvoidsetData(intdata)    {        this.data = data;    }    // Function to return the node"s value    publicintgetData()    {        returndata;    }}publicclassGFG {    // Function to clone the graph    publicGraph cloneGraph(Graph graph)    {        Map<GraphNode, GraphNode> map            = newHashMap<GraphNode, GraphNode>();        for(GraphNode node : graph.getNodes()) {            if(!map.containsKey(node))                cloneConnectedComponent(node, map);        }        Graph cloned = newGraph();        for(GraphNode current : map.values())            cloned.addNode(current);        returncloned;    }    // Function to clone the connected components    privatevoidcloneConnectedComponent(GraphNode node,                          Map<GraphNode, GraphNode> map)    {        Queue<GraphNode> queue = newLinkedList<GraphNode>();        queue.add(node);        while(!queue.isEmpty()) {            GraphNode current = queue.poll();            GraphNode currentCloned = null;            if(map.containsKey(current)) {                currentCloned = map.get(current);            }            else{                currentCloned = newGraphNode(current.getData());                map.put(current, currentCloned);            }            List<GraphNode> children = current.getChildren();            for(GraphNode child : children) {                if(map.containsKey(child)) {                    currentCloned.addChild(map.get(child));                }                else{                    GraphNode childCloned                        = newGraphNode(child.getData());                    map.put(child, childCloned);                    currentCloned.addChild(childCloned);                    queue.add(child);                }            }        }    }    // Function to build the graph    publicGraph buildGraph()    {        // Create graph        Graph g = newGraph();        // Adding nodes to the graph   &nbs
            РЕКОМЕНДУЕМЫЕ СТАТЬИ |