<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:10pt">Thanks experts!<br>Thanks Robert Kern!<br><br>Two more questions about it:<br><br>1. networkx.has_path(G, first_stick, second_stick) stop when find second_stick or compute all the sub-graph and then evaluate if first_stick and second_stick are connected?<br><br>2. Using networkx or other tool: how can I obtain the 'clusters size' distribution (that is: number of clusters of size 'D', for all cluster-sizes)?<br><br>Thanks a lot!!<br><br>José Luis<br><br><div style="font-family: times new roman, new york, times, serif; font-size: 10pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <hr size="1">  <font face="Arial" size="2"> <b><span style="font-weight:bold;">De:</span></b> Robert Kern <robert.kern@gmail.com><br> <b><span style="font-weight: bold;">Para:</span></b>
 Discussion of Numerical Python <numpy-discussion@scipy.org> <br> <b><span style="font-weight: bold;">Enviado:</span></b> miércoles, 4 de septiembre de 2013 18:49<br> <b><span style="font-weight: bold;">Asunto:</span></b> Re: [Numpy-discussion] Stick intersection path algorithm<br> </font> </div> <div class="y_msg_container"><br><div id="yiv6050979132"><div dir="ltr">On Wed, Sep 4, 2013 at 3:17 PM, Josè Luis Mietta <<a rel="nofollow" ymailto="mailto:joseluismietta@yahoo.com.ar" target="_blank" href="mailto:joseluismietta@yahoo.com.ar">joseluismietta@yahoo.com.ar</a>> wrote:<br>><br>> Hi experts!<br>><br>> If I do:<br>><br>> G = Graph(M)<br>

><br>> That is: to use the associated intersection graph, where the vertices are the sticks and there is an edge between the two vertices if they intersect. Two sticks are "connected by a 'intersected-stick' path" if they are in the same connected component of this graph.<br>

> It turns out that the matrix I consider (M) is the adjacency matrix of this graph.<br>><br>> Then, I can do:<br>><br>> first_stick in G.connected_component_containing_vertex(second_stick)<br>><br>> This is 'True' if 'first_stick' is in the sub-graph formed by 'second_stick' and all sticks 'connected' with 'second_stick'.<br>

><br>> The problem is that<br>><br>> G.connected_component_containing_vertex()<br>><br>> explore all the sub-graph.<br>><br>> How can I do (what is the code) for stop the iteration when the algorithm found 'first-stick'?<br>

<br>networkx.has_path(G, first_stick, second_stick)<div><br></div><div><a rel="nofollow" target="_blank" href="http://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.shortest_paths.generic.has_path.html#networkx.algorithms.shortest_paths.generic.has_path">http://networkx.github.io/documentation/latest/reference/generated/networkx.algorithms.shortest_paths.generic.has_path.html#networkx.algorithms.shortest_paths.generic.has_path</a><br>

<br>If you are going to be doing many queries, you should compute all of the path lengths, then you can query the final data structure easily.</div><br>lengths = networkx.all_pairs_shortest_path_length(G)<br>second_stick in lengths[first_stick]<br>

<br>--<br>Robert Kern</div><div class="yiv6050979132gmail_extra"><br><br><div class="yiv6050979132gmail_quote">On Wed, Sep 4, 2013 at 3:17 PM, Josè Luis Mietta <span dir="ltr"><<a rel="nofollow" ymailto="mailto:joseluismietta@yahoo.com.ar" target="_blank" href="mailto:joseluismietta@yahoo.com.ar">joseluismietta@yahoo.com.ar</a>></span> wrote:<br>

<blockquote class="yiv6050979132gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div style="font-size:10pt;font-family:times new roman, new york, times, serif;">Hi experts!<br><div><span><br></span></div>

<div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:times new roman, new york, times, serif;">If I do:</div><pre><code><span>G </span><span>=</span><span> </span><span>Graph</span><span>(</span><span>M</span><span>)<br>

<br></span></code></pre><div>That is: to use the associated <a rel="nofollow" target="_blank" href="https://en.wikipedia.org/wiki/Intersection_graph">intersection graph</a>,
 where the vertices are the sticks and there is an edge between the two 
vertices if they intersect. Two sticks are "connected by a 
'intersected-stick' path" if they are in the same connected component of
 this graph.</div><div>It turns out that the matrix I consider (M) is the <a rel="nofollow" target="_blank" href="https://en.wikipedia.org/wiki/Adjacency_matrix">adjacency matrix</a> of this graph.</div><div><br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:times new roman, new york, times, serif;">

Then, I can do:</div><pre><code><span>first_stick </span><span>in</span><span> G</span><span>.</span><span>connected_component_containing_vertex</span><span>(</span><span>second_stick</span><span>)</span></code></pre><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:times new roman, new york, times, serif;">

This is 'True' if 'first_stick' is in the sub-graph formed by 'second_stick' and all sticks 'connected' with
 'second_stick'.</div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:times new roman, new york, times, serif;"><br></div><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:times new roman, new york, times, serif;">

The problem is that <br></div><pre><code><span>G</span><span>.</span><span>connected_component_containing_vertex()<br><br></span></code></pre>explore <span style="font-weight:bold;"><span style="text-decoration:underline;">all </span></span>the sub-graph.<br>

<br>How can I do (what is the code) for stop the iteration when the algorithm found 'first-stick'? <br><br>Waiting for your answers.<br><br>Thanks a lot!!<br><div style="font-style:normal;font-size:13.3333px;background-color:transparent;font-family:times new roman, new york, times, serif;">

<br><span></span></div><br><div style="font-family:times new roman, new york, times, serif;font-size:10pt;"> <div style="font-family:times new roman, new york, times, serif;font-size:12pt;"> <div dir="ltr"> <hr size="1">  <font face="Arial"> <b><span style="font-weight:bold;">De:</span></b> Robert Kern <<a rel="nofollow" ymailto="mailto:robert.kern@gmail.com" target="_blank" href="mailto:robert.kern@gmail.com">robert.kern@gmail.com</a>><div class="yiv6050979132im">

<br> <b><span style="font-weight:bold;">Para:</span></b> Discussion of Numerical Python <<a rel="nofollow" ymailto="mailto:numpy-discussion@scipy.org" target="_blank" href="mailto:numpy-discussion@scipy.org">numpy-discussion@scipy.org</a>> <br> </div><b><span style="font-weight:bold;">Enviado:</span></b> lunes, 2 de septiembre de 2013 10:40<div class="yiv6050979132im">

<br> <b><span style="font-weight:bold;">Asunto:</span></b> Re: [Numpy-discussion] Stick intersection path algorithm<br> </div></font> </div> <div><div><div class="yiv6050979132h5"><br><div><div dir="ltr">On Sun, Sep 1, 2013 at 11:55 PM, Josè Luis Mietta <<a rel="nofollow" ymailto="mailto:joseluismietta@yahoo.com.ar" target="_blank" href="mailto:joseluismietta@yahoo.com.ar">joseluismietta@yahoo.com.ar</a>> wrote:<br>

><br>> Hi experts!<br>><br>> I wanna study the intersection between line segments (sticks).<br>


> I wrote a algorithm that generate a matrix, M, with N rows and N columns. The M-element Mij is 1 if stick number 'i' intersect stick number 'j' (obviously M is symmetric).<br>><br>> Given two arbitrary sticks, i need a simple and effective algorithm that determinate if that two sticks are conected by a 'intersected-sticks' path.<br>




<br>You may find it helpful to reword your problem using standard terminology. If I hadn't read your previous question, I would not have been able to understand what you meant by intersected sticks (or, as Chris thought at first, that you needed help determining the intersections). This will also help in searching Google for background and pre-existing software to solve your problem.<br>



<br>You have an "undirected graph" (the sticks are "nodes", and the intersections are "edges") and you want to find if two given nodes are "reachable" from each other. You are currently representing your graph as an "adjacency matrix" `M` where `M[i,j]` is 1 iff nodes `i` and `j` have an edge between them and 0 otherwise. The "transitive closure" of your graph `M` is the graph that connects two nodes with an edge iff the two nodes are reachable from each other in the original graph `M`. There are several graph theory packages out there, like NetworkX, that will do this for you. Depending on the kinds of queries you would like to do, as David points out, want to compute the "connected components" of your graph; a connected component is a subgraph of your original graph such that all of the nodes are reachable from each other.<div>



<br></div><div>You can also look up Python code for computing the transitive closure of a graph; it's not a complicated algorithm. However, this algorithm is usually implemented using a different representation of a graph than an adjacency matrix, so you may need to do a conversion.</div>



<div><br><div>--<br>Robert Kern<br></div></div></div></div><br></div></div><div class="yiv6050979132im">_______________________________________________<br>NumPy-Discussion mailing list<br><a rel="nofollow" ymailto="mailto:NumPy-Discussion@scipy.org" target="_blank" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>

<a rel="nofollow" target="_blank" href="http://mail.scipy.org/mailman/listinfo/numpy-discussion">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br><br><br></div></div> </div> </div>  </div></div><br>_______________________________________________<br>


NumPy-Discussion mailing list<br>
<a rel="nofollow" ymailto="mailto:NumPy-Discussion@scipy.org" target="_blank" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a rel="nofollow" target="_blank" href="http://mail.scipy.org/mailman/listinfo/numpy-discussion">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Robert Kern
</div></div><br>_______________________________________________<br>NumPy-Discussion mailing list<br><a ymailto="mailto:NumPy-Discussion@scipy.org" href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br><a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br><br><br></div> </div> </div>  </div></body></html>