Killing a random directed link in both direction in netlogo - network-programming

Greeting,
I am very new to netlogo, and is very interested in GIS and network feature in netlogo.
Currently, I have constructed a directed graph network.
I would like to implement a function to kill a directed link path in both direction.
I have found way to kill a link in both direction if you know which are the linkage of the 2 node to kill off. Below is the code for that (by Seth Tisue in one of the question thread) :
ask a [ ask link-with b [ die ] ]
I would like to kill of a random linkage in both direction. I assume I would need to use "ask one-of links [ die ]" and another part of the code to kill of the corresponding other direction of the linkage.
I guess my question would be how the mechanism of "one-of" work so that I would be able to store this random link first, and then kill off both direction using the first code above.
As a side line, I am also puzzled by how netlogo work with variable which is quite unlike normal C++ program(my fundamental is with c++ coding). I do quite a lot of importing for my network and GIS shapefile, and currently I am still not using netlogo engine as my computational engine(a waste for me).
How do you store a simple "x=1+1" into a global variable in netlogo?
Really sorry if this question is too simple for most.

Just as a general stackoverflow convention, if you have two questions, please ask them as separate questions. This is to make stackoverflow a useful resource for programmers, so that the title and question and answer are both together and easily found.
First, how do you even know there is a link in both directions? Typically directed graphs have many pairs of nodes where there is A to B or B to A but not both. The code below tests the other direction and kills both.
I have included your example of setting variable x to the value of 1 + 1 in the setup.
I am convinced there is a better way of doing this as NetLogo code is usually less ugly, but I can't find it. The following code is a complete runnable example that creates a bunch of nodes and links and then kills a link (including the reciprocal if it exists) whenever the kill-link procedure is run.
globals [x tonode fromnode]
to setup
clear-all
create-turtles 20
[ setxy random-xcor random-ycor]
repeat 100
[ ask one-of turtles
[ create-link-to one-of other turtles ; will not be created if already link
]
]
set x 1 + 1
end
to kill-link
ask one-of links
[ set fromnode end1
set tonode end2
die
]
ask tonode
[ if out-link-neighbor? fromnode
[ ask out-link-to fromnode [die]
]
]
end

Global variable should be declared at the beginning of program:
globals [
my-global-link; this will be my global variable
my-another-one-link; another one
]
To "store" random link use it like:
to do-something
set my-global-link one-of links
end
For local variables (inside a procedure) just use let do declare it first:
to do-something
let tmp-global-link one-of links
end

Related

Find turtles that are not connected to others in netlogo

I have a network with near of 4700 turtles, however not all of them are connected, and that is ok..... but I need to find the ones that are not connected to others (through links) and kill them, how can I do it?
I imagine something like:
""ask turtles with [no-links] [die]""
But... it doesn't work because no-links reports nothing!!... I dont know if it is the correct way (changing the with condition) or I must follow another step.
PD: all the turtles and their links are set up in a nw context.
If you take a look into the netlogo dictionairy, you will see that no-links reports an empty link agentset.
There are two reporters you can use for getting the number of links for each turtle: link-neighbors or my-links:
ask turtles with [not any? my-links] [die]
or
ask turtles with [not any? link-neighbors] [die]

Detecting cycle in a singly linked list

Detecting cycles in a single linked list is a well known problem. I know that this question has been asked a zillion times all over the internet. The reason why I am asking it again is I thought of a solution which I did not encounter at other places. (I admit I haven't searched that deeply either).
My solution is:
Given a linked list and pointer to some node, break the link between node and node->next();
Then start at node->next() and traverse till either you hit an end (which means there was no loop) or till you reach at node which means there was a loop.
Is there anything wrong/good about above solution ?
Note: Do join the link back once you are done.
That will work to detect complete cycles (i.e., cycles with a period of the whole list), e.g.:
A -> B -> C -> D -> A
But what if we have a cycle somewhere else in the list?
e.g.,
A -> B -> C -> D -> E -> C
I can't see that your algorithm will detect the cycle in this case.
Keep in mind that to detect the first case, we need not even break the link. We could just traverse the list and keep comparing the next link for each node with the head element to see if we'd started back at the start yet (or hit the end).
I guess the most trivial approach (not necessarily the best, but one that everybody should know how to implement in Java in a few lines of code) is to build a Hash Set of the nodes, start adding them until you find one that you already saw before. Takes extra memory though.
If you can mark nodes, start marking them until you find one you marked before (the hash map is essentially an external marker).
And check the usual graph theory books...
You are not allowed to break a link, even if you join it back at the end. What if other programs read the list at the same time ?
The algorithm must not damage the list while working on it.

Find all paths between two nodes

Using a gremlin script and neo4j I try to find all paths between two nodes, descending at most 10 levels down. But all I get as response from the REST API is a
java.lang.ArrayIndexOutOfBoundsException: -1
Here is the script:
x = g.v(2)
y = g.v(6)
x.both.loop(10){!it.object.equals(y)}.paths
I looked through the documentation, but couldnt find anything relevant for this usecase.
In Gremlin the argument to loop is the number of steps back that you wish to go and the closure is evaluated to determine when to break out of the loop. In this case, because you have loop(10) it's going to go back way too far to a point where the pipeline is not defined. With the respect to the closure, you'll need to check not only if the object is the one in question, in which case you should stop, but also whether or not you've done 10 loops already.
What you really want it something like this:
x.both.loop(1){!it.object.equals(y) && it.loops < 10}.paths
However, I should add that if there is a cycle in the graph, this will gladly traverse the cycle over and over and result in far too many paths. You can apply some clever filter and sideEffect to avoid visiting nodes multiple times.
For more information see the Loop Pattern Page on the Gremlin Wiki.

How can I remember which data structures are used by DFS and BFS?

I always mix up whether I use a stack or a queue for DFS or BFS. Can someone please provide some intuition about how to remember which algorithm uses which data structure?
Queue can be generally thought as horizontal in structure i.e, breadth/width can be attributed to it - BFS, whereas
Stack is visualized as a vertical structure and hence has depth - DFS.
Draw a small graph on a piece of paper and think about the order in which nodes are processed in each implementation. How does the order in which you encounter the nodes and the order in which you process the nodes differ between the searches?
One of them uses a stack (depth-first) and the other uses a queue (breadth-first) (for non-recursive implementations, at least).
I remember it by keeping Barbecue in my mind. Barbecue starts with a 'B' and ends with a sound like 'q' hence BFS -> Queue and the remaining ones DFS -> stack.
BFS explores/processes the closest vertices first and then moves outwards away from the source. Given this, you want to use a data structure that when queried gives you the oldest element, based on the order they were inserted. A queue is what you need in this case since it is first-in-first-out(FIFO).
Whereas a DFS explores as far as possible along each branch first and then bracktracks. For this, a stack works better since it is LIFO(last-in-first-out)
Take it in Alphabetical order...
.... B(BFS).....C......D (DFS)....
.... Q(Queue)...R......S (Stack)...
BFS uses always queue, Dfs uses Stack data structure. As the earlier explanation tell about DFS is using backtracking. Remember backtracking can proceed only by Stack.
BFS --> B --> Barbecue --> Queue
DFS --> S --> Stack
Don't remember anything.
Assuming the data structure used for the search is X:
Breadth First = Nodes entered X earlier, have to be generated on the tree first: X is a queue.
Depth First = Nodes entered X later, must be generated on the tree first: X is a stack.
In brief: Stack is Last-In-First-Out, which is DFS. Queue is First-In-First-Out, which is BFS.
Bfs;Breadth=>queue
Dfs;Depth=>stack
Refer to their structure
The depth-first search uses a Stack to remember where it should go when it reaches a dead end.
DFSS
Stack (Last In First Out, LIFO). For DFS, we retrieve it from root to the farthest node as much as possible, this is the same idea as LIFO.
Queue (First In First Out, FIFO). For BFS, we retrieve it one level by one leve, after we visit upper level of the node, we visit bottom level of node, this is the same idea as FIFO.
An easier way to remember, especially for young students, is to use similar acronym:
BFS => Boy FriendS in queue (for popular ladies apparently).
DFS is otherwise (stack).
I would like to share this answer:
https://stackoverflow.com/a/20429574/3221630
Taking BFS and replacing a the queue with a stack, reproduces the same visiting order of DFS, it uses more space than the actual DFS algorithm.
You can remember by making an acronym
BQDS
Beautiful Queen has Done Sins.
In Hindi,
बहुरानी क्यु दर्द सहा
Here is a simple analogy to remember. In a BFS, you are going one level at a time but in DFS you are going as deep as possible to the left before visiting others. Basically stacking up a big pile of stuff, then analyze them one by one, so if this is STACK, then the other one is queue.
Remember as "piling up", "stacking up", big as possible. (DFS).
if you visually rotate 'q' symbol (as in queue) 180 degrees you will get a 'b' (as in bfs).
Otherwise this is stack and dfs.

A simple explanation of Rings in Riak?

I'm trying to understand what the "rings" in Riak are, but I can't seem to find a clear explanation (please don't just link me to a web site, I have already read what is on the web). As far as I understand a node in Riak is a partition in a ring. Is that correct?
I know you've said you read everything on the web already, but for others also reading this question, I'd like to post two resources that discuss Riak's "ring":
http://riak.basho.com/arch.html
http://riak.basho.com/edoc/architecture.txt
With that out of the way, Riak uses the word "ring" in two places. The first is to describe the hash space that is used for determining where to store data. The reason Riak calls that space a ring is that the last value in the space (2^160-1) is thought of as being adjacent to the first value in the space (0). Replicas of data are stored in the "next N partitions" of the hash space, following the partition to which the key hashes. Considering the hash space as a ring gives a convenient definition for the "next parition after the final partition."
The other use of the word "ring" is related to, but not exactly the same as the former. I mentioned partitions: each node claims several segments of the hash space, called partitions. Knowledge about which node has claimed which partition is stored in a structure that Riak calls the "ring state", or sometimes just the "ring." Other cluster metadata may also be kept in the ring state, because it's a conveniently shared piece of data throughout the cluster.
In general usage, an application shouldn't need to think about the ring much.
Does that answer your question? I'd encourage you to post questions like this to the riak-users mailing list ( http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com ). I'm sure other members of that list are interested in these answers, and we (the Riak developers) tend to be more attentive to that feed than this one.

Resources