I have a database of several polygones. Each polygon has a lot of nodes which define it. Additionally each polygone has at least one neighbour. For a calculation I need to determine the length of the shared edge of two neighboured polygones. In the example picture such a case is shown. The two polygones share the red edges. How can I calculate the length of the red edge with the help of PostGIS? I didn't find a function for that.
You can get the intersection of two polygons, then get the length(s) of any LineStrings. For example, take two geometries:
SELECT ST_Length(ST_CollectionExtract(ST_Intersection(a_geom, b_geom), 2))
FROM (
SELECT
'POLYGON((70 170,160 250,240 220,270 130,185 62,90 80,70 170))'::geometry AS a_geom,
'POLYGON((160 250,236 314,380 290,390 150,270 130,240 220,160 250))'::geometry AS b_geom
) f;
Or do this for all pairs of polygons in a table mypoly that touch:
SELECT a.gid AS gid_a, b.gid AS gid_b,
ST_Length(ST_CollectionExtract(ST_Intersection(a.geom, b.geom), 2))
FROM mypoly a, mypoly b
WHERE a.gid < b.gid AND ST_Touches(a.geom, b.geom);
You can also explore the Topology extension, which is part of PostGIS 2.x.
Related
I got this exercise :
Give an example of three different directed graphs on V = {1, 2, 3}
where the edges (1, 2) and (1, 3) exist. Write both the adjacency-list and adjacency-
matrix representation for each.
I find only this two:
G = {(1,2), (1,3)}
G = {(1,2), (1,3), (2,3)}
What I miss? Something like that is valid: G = {(1,2), (1,3), (3,2)} ?
It's a directed graph, which means all links are one-way. If you want to be able to go from 1 to 2 and from 2 to 1, you need two links, (1,2) and (2,1) - that's part of the definition of a directed graph.
With that, enumerate all possible links for a graph of 3 vertexes:
(1,2)
(2,1)
(1,3)
...
Once you enumerate all possible links in such a graph, you can pick and choose unique sets of those links to make into multiple graphs, subject to the constraints given to you by the exercise.
For instance, here are a couple graphs:
{(1,2)}
{(2,1)}
{(1,3)}
{(3,1)}
{(3,2), (2,1), (1,3)}
...
You already have two out of three requested answers and you need a third graph to complete the exercise. You need to give answers that include two provided links. Why not give as an answer a graph that has every link in it? A graph of every link must contain the two requested links, right?
I am new to igraph and graph theory. I have a very large file (> 4 GB) and I was told it is a graph. I can see the format includes the pairs separated by tab and I can read it as a table first then convert it to graph data frame.
Number of vertices with vcount and number of edges with ecount suggest that there are vertices with multiple edges. I have been looking at various sources but I could not find the information about directly extracting the vertices with more than one edges.
Any help is appreciated.
To get the edges incident to each vertex (if g is your igraph)
ie <- igraph::incident_edges(g, igraph::V(g))
Then, to get the number of edges adjacent to each vertex
num.incident.edges <- sapply(ie, length)
Sorry, I guess I was wrong with the terminology. What I meant by vertices with multiple edges is called 'articulation_points'.
This was what I was looking for:
library(igraph)
bi <- biconnected_components(g)
bi$articulation_points
I've been given the following exercise: There's an unweighted, directed, weakly connected graph with n nodes (n < 1 000 000). We want to traverse the whole graph, starting from the least number of nodes. The question is: from which nodes do I start the traversals? I couldn't find any content on this particular topic. However, I managed to come up with an algorithm, but it's not efficient enough:
I store the graph in an adjacency list (n can be too high for a two-dimensional matrix)
I start a BFS from each node i, and store the nodes it reached in x[i][...] (x = List<List<int>>)
I check whether any x[i].Count == n
I check whether any (x[i] union x[j]).Count == n
I check whether any (x[i] union x[j] union x[k]).Count == n
... So I make all possible unions of 2, 3, 4... subsets of x, and check whether its count is n.
It works all right if n is not too high, but I would need a more efficient algorithm for bigger n.
Any help is appreciated (you would make me be able to fall asleep again)! :)
Find the nodes that do not have any incoming edges. Loop over these nodes, and for each node v, begin traversing the graph. Remember which nodes you visited (by putting them in a hash table or marking them). Stop traversing when you reach a node you have already visited.
You would need an adjacency list representation, where each node has a list of incoming and a list of outgoing edges. Then do something like this:
Set nodesToVisit = emptySet;
for i=1 to n:
if incoming[i].size() == 0:
nodesToVisit.add(i)
Set visited = emptySet;
for v in nodesToVisit:
nodesToVisit.remove(v)
if(v is not in visited):
visit(v);
visited.add(v);
for u in outgoing[v]:
nodesToVisit.add(u)
I am wondering if someone have done this already, to send me into right direction..
The issue is as follow : I have a 2 dimensional array, on which i hold integer numbers, if the number is 0 - the item shall not be included into the graph, if it is 1 - it must be included.
The result graph shall be used for patfinding ( the shortest path ) to some element.
how to turn this 2 dimensional array into the graph ? ( with polygona.de classes if possible ),
I am currently trying with Polygonal.de classes. any suggestions and points into the right direction is more than appreciated.
This is the 2 dimensional structure. The red cells is prohibited to walk on, and there must be found optimal path from "start" to the "end". But 1st things 1st - i need to turn this 2 dimensional structure into a graph now :|
The way I see it, your 2D array is already a graph. A node of the graph is represented by a pair (i, j) and may have neighbor nodes such as (i + 1, j), (i, j + 1), etc. You can write a utility function for your array that hides these low-level neighbor definitions and skips the cells that are occupied.
The de.polygonal.ds API for the Graph data structure contains this example for the construction of a graph:
var graph = new de.polygonal.ds.Graph<String>();
var a = graph.addNode("a");
var b = graph.addNode("b");
var c = graph.addNode("c");
graph.addSingleArc(a, b, 1.0);
graph.addSingleArc(b, a, 1.0);
graph.addMutualArc(a, c, 1.0);
Adjust the example to construct a 2D array that contains a node for each free (i, j) of the original 2D array. Then traverse the 2D array of nodes and call addMutualArc() to connect adjacent nodes.
I have two graphs G, H labeled and I want to extract all common subgraph of two graphs, I got to a part that is:
1 - extract all the nodes that are in common, but I'm stuck on the part that includes:
2 - Step 1: Take the First vertex and store it in a set P = {first element} (which will be the set of all common subgraph), and go to 2nd if it is adjacent to the first of the two P graph G and H, we add it, and so on, but I do not know how to do it when i have more than 2
That is a NP-complete problem. See http://en.wikipedia.org/wiki/Subgraph_isomorphism_problem