Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to print undirected graph like LeetCode's serialisation? #4

Closed
pezy opened this issue Jan 25, 2015 · 1 comment
Closed

How to print undirected graph like LeetCode's serialisation? #4

pezy opened this issue Jan 25, 2015 · 1 comment
Assignees

Comments

@pezy
Copy link
Owner

pezy commented Jan 25, 2015

OJ's undirected graph serialization:
Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.
As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1. Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.
    Visually, the graph looks like the following:
   1
  / \
 /   \
0 --- 2
     / \
     \_/

Question:
Give you an undirected graph like above, print a string like 0,1,2#1,2#2,2.

Another example: 0,1,5#1,2,5#2,3#3,4,4#4,5,5#5, check this.

@pezy pezy self-assigned this Jan 25, 2015
@pezy
Copy link
Owner Author

pezy commented Jan 25, 2015

BFS in sorted way.

#include <iostream>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <deque>
#include <string>
#include <sstream>

void printUndirectedGraph(UndirectedGraphNode *node) {
    std::ostringstream out;
    std::unordered_map<int, std::unordered_set<int>> map{{0, std::unordered_set<int>()}};
    std::deque<UndirectedGraphNode*> cache;
    cache.push_back(node);
    do {
        std::sort(cache.begin(), cache.end(),[](UndirectedGraphNode* n1, UndirectedGraphNode* n2){
            return n1->label < n2->label;
        });
        UndirectedGraphNode *node = cache.front(); cache.pop_front();
        out << node->label;
        auto found = map.find(node->label);
        for (UndirectedGraphNode* neighbor : node->neighbors) {
            if (map.find(neighbor->label) == map.end()) cache.push_back(neighbor);
            if (found != map.end()) {
                std::unordered_set<int> &exist = found->second;
                if (exist.find(neighbor->label) == exist.end()) out << "," << neighbor->label;
            }
            map[neighbor->label].emplace(node->label);
        }
        out << "#";
    } while (!cache.empty());
    std::string out_str = out.str();
    std::cout << out_str.substr(0, out_str.size()-1) << std::endl;
}

@pezy pezy closed this as completed Feb 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant