由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 图的拷贝
相关主题
Depth-First-Search一道linkedin的graph题
Amazon面试题请教请教一道面试题
Course Schedule II 变形题怎么做?也问一个算法题
Amazon电面经算法题:两列找共同元素有O(n)的算法吗?
bloomberg onsite题5分钟前G的电面
问道题,binary tree里有一个有indegree 2如何把一个文件 copy to multiple machine efficiently?
报Google Offer并请教面试题[合集] Google Phone Interview
在版上看到的G题[合集] Amazon Onsite 面试题
相关话题的讨论汇总
话题: node话题: graph话题: neighbour话题: array话题: 拷贝
进入JobHunting版参与讨论
1 (共1页)
S**Y
发帖数: 136
1
一个graph;
class Node
{
priavte:
string vale;
Vector neghbors;
}
问你如何拷贝这个graph?
还挺新颖的,谁给个标准解答?
g*******y
发帖数: 1930
2
hash;
m*****f
发帖数: 1243
3
1. traverse the graph use dfs or bfs, make a new object for every node, do a
shallow copy, but push the address of the new object into the neighbor
vector.
2. relink. For each node.
neighbour[x] = neighbour[x]->neighbour[neighbour[x]->neighbour.size()-1]
3. delete the original graph
It's the same idea of the LL with random pointer problem. The key is using
previous data structure to save a link of the new object, copy->relink->
delete.
S**Y
发帖数: 136
4
我根据你写的,写下算法,帮我看看对不对:
从source Node开始,用bfs(use a queue),进行traversal,
同时keep一个hash table;
int是一个编号,比如source为1,其邻居为2,3,4
处理source Node 为 一串string-> 1:str:2:3:4 str为节点value直,2/3/4为邻居的编
号, 并且根据编号放进一个array of string ARRAY中,
当bfs traversal到一个Node的时候,先检查他是否在hashTable中,如果不在就hash ode,编号>到hash table,
并且生成i:string:x:y:z, 放进 ARRAY 中。
其中在第一次discover新Node的时候,只是生成这个新Node的编号,并放进 ARRAY 中,
并不处理它的邻居。只是后来它从Queue pop出来的时候,才从ARRAY中寻找到它(根据
hash table),并且更新它的邻居。
is this correct?

【在 g*******y 的大作中提到】
: hash;
1 (共1页)
进入JobHunting版参与讨论
相关主题
[合集] Amazon Onsite 面试题bloomberg onsite题
请教一个binary search tree和heap的问题。问道题,binary tree里有一个有indegree 2
问个我不太理解的问题--在别的地方看来的报Google Offer并请教面试题
从tree的post order traversal和pre,能否build这个tree?在版上看到的G题
Depth-First-Search一道linkedin的graph题
Amazon面试题请教请教一道面试题
Course Schedule II 变形题怎么做?也问一个算法题
Amazon电面经算法题:两列找共同元素有O(n)的算法吗?
相关话题的讨论汇总
话题: node话题: graph话题: neighbour话题: array话题: 拷贝