确定此算法的Big-O(Determining the Big-O of this algorithm)
我试图了解下面代码中的Big-O是什么。
代码应该做什么
本质上,我试图选择随机节点的子集(max size = selectionSize)以及它们之间存在的任何边。 随机节点的选择在
while
循环中完成。 完成后,我想选择所选节点之间存在的任何边。我认为是什么和为什么
我认为运行时间是O = n^2
,其中n=selectionSize
。原因是:即使我可以增加nodes
元素的大小(例如使其为10000),我不相信它会影响算法,因为我只是循环遍历selectionSize
的最大值。我有点担心这是错误的唯一原因是因为while
循环,我从列表中选择随机元素,直到我有足够的。虽然这可能需要很长时间(因为它是随机的),但我认为它不会影响整体输出的时间。编辑 :呃第二个想法......没关系......nodes
大小确实影响它(因为node.getNeighbors()
最多可以是nodes
的大小)..所以我认为如果selectionSize
等于大小nodes
的运行时间为O=n^2
,其中n=size of nodes
。任何提示/提示将不胜感激。
码
// nodes and selectionSize are my input: int[] nodes = {1,2,3...,1000}; // 1000 elements int selectionSize = 500; // This can be at most the size of the elements (in this case 1000) run_algo(nodes, selectionSize); public void run_algo(int[] nodes, int selectionSize) { randomNodesList = {}; while(randomNodesList < selectionSize) { randomNode = selectRandomNode(nodes); // Assume O(1) if(!randomNodesList.exists(randomNode)) { // Assume O(1) randomNodesList.push_back(randomNode); // Assume O(1) } } foreach(node in randomNodesList) { foreach(neighbor in node.getNeighbors()) { // Max. nodes size (in this case 1000) if (!randomNodesList.exists(neighbor)) { // Assume O(1) AddEdge(node, neighbor); // Takes O(1) time } } } }
I'm trying to understand what the Big-O is of the code below.
What the code is supposed to do
Essentially, I am trying to select a subset (max size = selectionSize) of random nodes and any edges that exists between them. The selection of random nodes is done in the
while
loop. After having done that, I want to select any edges that exist between the selected nodes.What I think it is & why
I think the running time isEdit: ugh on second thoughts... Nevermind... TheO = n^2
wheren=selectionSize
. The reason is: even though I can increase the size of the elements innodes
(e.g. make it 10000), I don't believe it can affect the algorithm since I am only looping through the maximum ofselectionSize
. The only reason I am a bit worried that this is wrong is because of thewhile
loop, where I select random elements from the list up until I have enough. While this can take quite long (because it is random), I don't think it affects the overall output in terms of time.nodes
size does affect it (sincenode.getNeighbors()
can be at most the size of thenodes
).. So I think that if theselectionSize
is equal to the size ofnodes
, the running time isO=n^2
wheren=size of nodes
.Any tips/hints would be appreciated.
Code
// nodes and selectionSize are my input: int[] nodes = {1,2,3...,1000}; // 1000 elements int selectionSize = 500; // This can be at most the size of the elements (in this case 1000) run_algo(nodes, selectionSize); public void run_algo(int[] nodes, int selectionSize) { randomNodesList = {}; while(randomNodesList < selectionSize) { randomNode = selectRandomNode(nodes); // Assume O(1) if(!randomNodesList.exists(randomNode)) { // Assume O(1) randomNodesList.push_back(randomNode); // Assume O(1) } } foreach(node in randomNodesList) { foreach(neighbor in node.getNeighbors()) { // Max. nodes size (in this case 1000) if (!randomNodesList.exists(neighbor)) { // Assume O(1) AddEdge(node, neighbor); // Takes O(1) time } } } }
原文:https://stackoverflow.com/questions/44936927
最满意答案
事实证明这是因为Node安装了选项--without-ssl。 这意味着它不会安装socket.it所需的任何加密内容。 我安装了openssl,重新安装了Node,并修复了它:)
It turns out it's because Node was installed with the option --without-ssl. This means it doesnt install any of the crypto stuff, which was needed by socket.it. I installed openssl, reinstalled Node, and this fixed it :)
相关问答
更多-
实际上并没有向其他客户端发送更新,而是仅仅发送给刚刚连接的客户端(这是您首次加载时看到更新的原因) // socket is the *current* socket of the client that just connected socket.emit('users_count', clients); 而是要发送到所有套接字 io.sockets.emit('users_count', clients); 或者,您可以使用广播功能,该功能向除了启动它的套接字之外的所有人发送消息: socket. ...
-
好的,所以有一些事情: 首先, var mSocket似乎没有被初始化,所以它可能很难发出()任何东西(我错过了什么?) 第二,当你这样做时: socket.on('pong', function (data) { console.log(data.message); }); 服务器期望接收包含message属性的对象,例如: data = {message:'hi server'}在您的情况下,您发送一个字符串,因此data是'Hi server !' 你的日志会说“未定义”。 您应该将此位更改 ...
-
this.client.on("event",function(data) { console.log(this.value === "some random value"); // true }.bind(this)); bind使 this关键字设置为提供的值,即User对象。 this.client.on("event",function(data) { console.log(this.value === "some random value"); // true }.bind( ...
-
事实证明这是因为Node安装了选项--without-ssl。 这意味着它不会安装socket.it所需的任何加密内容。 我安装了openssl,重新安装了Node,并修复了它:) It turns out it's because Node was installed with the option --without-ssl. This means it doesnt install any of the crypto stuff, which was needed by socket.it. I in ...
-
我最终设法解决了我的连接问题,我的解决方案主要围绕最佳服务器配置。 Node.js代码(index.js) var arguments = process.argv.slice(2); if( ! arguments[0]) { console.log("Error: Missing port."); process.exit(1); } var serverPort = arguments[0]; var redisHost = '127.0.0.1'; var redisP ...
-
您正在丢失可以尝试绑定的 上下文 : var game_room = function(){ this.socket = io.connect(); this.socket.on('connect', function(){ this.socket.emit('message', 'Hello server'); }.bind(this)); } 或者您可以将this.socket设置为一些本地var: var game_room = function(){ ...
-
恐怕重新连接算法不能修改(截至2013年12月); 允许此功能的Github问题尚未合并。 然而,其中一位评论者提出了一个小的解决方法,它应该使指数增长无效: 关于reconnecting socket.socket.reconnectionDelay /= 2 正如你所说,另一种方法是编写一些客户端代码来覆盖重新连接行为,并进行轮询。 这是一个如何做到这一点的例子 。 编辑:上面的代码将不得不去'断开'事件回调: var socket = io('http://localhost'); socket.on ...
-
我能够通过以下方式了解这一点: 订阅websocket到多个房间 在每个POST处附加其他变量以定义要发射到的房间 我能够确定: 使用相同的主机将始终保持相同的会话ID /套接字(即使启用了强制连接) 可能使用不同的主机或命名空间将允许连接分离 最好与多个房间保持一个连接 I was able to figure this out by: Subscribing the websocket to multiple rooms Appending additional variables at each PO ...
-
尝试 users.push(socket); // without .sessionId for (var u in users) { // users[u] is now the socket console.log(users[u].id); users[u].emit('message', { msg: 'New User Connected succesfully' }); } Try users.push(socket); // without .sessionId ...
-
socket.io在连接时不会将事件从服务器发送到客户端(socket.io not emitting event from server to client on connection)[2022-07-19]
您需要使用socket.on(event, callback);来监听发出的事件socket.on(event, callback);