How to calculate the maximum degree of a directed graph using SPARQL?

I calculated the indegree and outdegree values ​​of each node in a directed graph in two separate queries:

SELECT ?s (COUNT(*) AS ?outdegree) 
{ ?s ?p ?o }
GROUP BY ?s
ORDER BY DESC(?outdegree) 

SELECT ?o (COUNT(*) AS ?indegree) 
{ ?s ?p ?o }
GROUP BY ?o
ORDER BY DESC(?indegree)  

I need to calculate the maximum degree of a graph. Since the maximum degree of a directed graph is the maximum (independent + external) value of the graph, I want to know how to combine the results of these two queries to calculate it.

In addition, if there is a more efficient way to do this, offer them as well.

+4
source share
2 answers

You can use a fairly simple query to get the degree of each vertex ?x:

select ?x (count(*) as ?degree) { 
  { ?x ?p ?o } union
  { ?s ?p ?x }
}
group by ?x

For example, from this data:

@prefix : <https://stackoverflow.com/q/24270532/1281433/> .

#     a
#     |
#     V
# b<--c-->d
#     |
#     V  
#     e

:a :p :c .
:c :p :b, :d, :e .

:

---------------
| x  | degree |
===============
| :a | 1      |
| :b | 1      |
| :c | 4      |
| :d | 1      |
| :e | 1      |
---------------

, , 1, ,

select ?x (count(*) as ?degree) { 
  { ?x ?p ?o } union
  { ?s ?p ?x }
}
group by ?x
order by desc(?degree)
limit 1
---------------
| x  | degree |
===============
| :c | 4      |
---------------

, . , .

, , - Rob Hall answer , , , , 0 0 outdegree, , . , , node . , Jena.

+3

:

<urn:ex:cent0>  <urn:ex:p>  <urn:ex:cent1> , <urn:ex:o1> , <urn:ex:o0> .
<urn:ex:s1>  <urn:ex:p>  <urn:ex:cent0> .
<urn:ex:cent1>  <urn:ex:p>  <urn:ex:o3> , <urn:ex:o2> .
<urn:ex:s2>  <urn:ex:p>  <urn:ex:cent0> .
<urn:ex:s0>  <urn:ex:p>  <urn:ex:cent0> .

:

SELECT  ?cent (( ?indegree + ?outdegree ) AS ?degree)
WHERE
  { { SELECT  (?s AS ?cent) (count(*) AS ?outdegree)
      WHERE
        { ?s ?p ?o }
      GROUP BY ?s
      ORDER BY DESC(?outdegree)
    }
    { SELECT  (?o AS ?cent) (count(*) AS ?indegree)
      WHERE
        { ?s ?p ?o }
      GROUP BY ?o
      ORDER BY DESC(?indegree)
    }
  }

:

-----------------------------
| o              | indegree |
=============================
| <urn:ex:cent0> | 3        |
| <urn:ex:cent1> | 1        |
| <urn:ex:o0>    | 1        |
| <urn:ex:o1>    | 1        |
| <urn:ex:o2>    | 1        |
| <urn:ex:o3>    | 1        |
-----------------------------
------------------------------
| s              | outdegree |
==============================
| <urn:ex:cent0> | 3         |
| <urn:ex:cent1> | 2         |
| <urn:ex:s0>    | 1         |
| <urn:ex:s1>    | 1         |
| <urn:ex:s2>    | 1         |
------------------------------
---------------------------
| cent           | degree |
===========================
| <urn:ex:cent0> | 6      |
| <urn:ex:cent1> | 3      |
---------------------------

node . , ( , ):

final Resource c0 = ResourceFactory.createResource("urn:ex:cent0");
final Resource c1 = ResourceFactory.createResource("urn:ex:cent1");
final Property p = ResourceFactory.createProperty("urn:ex:p");

final Model model = new ModelCom(Factory.createDefaultGraph()){{
    this.add(this.createResource("urn:ex:s0"), p, c0);
    this.add(this.createResource("urn:ex:s1"), p, c0);
    this.add(this.createResource("urn:ex:s2"), p, c0);

    this.add(c0, p, this.createResource("urn:ex:o0"));
    this.add(c0, p, this.createResource("urn:ex:o1"));
    this.add(c0, p, c1);

    this.add(c1, p, this.createResource("urn:ex:o2"));
    this.add(c1, p, this.createResource("urn:ex:o3"));
}};

final Query outdeg = QueryFactory.create(
    "SELECT ?s (COUNT(*) AS ?outdegree)\n"+ 
    "{ ?s ?p ?o }\n"+
    "GROUP BY ?s\n"+
    "ORDER BY DESC(?outdegree)"
);

final Query indeg = QueryFactory.create(
    "SELECT ?o (COUNT(*) AS ?indegree)\n"+ 
    "{ ?s ?p ?o }\n"+
    "GROUP BY ?o\n"+
    "ORDER BY DESC(?indegree)"
);

final Query alldeg = QueryFactory.create(
    "SELECT ?cent ((?indegree+?outdegree) AS ?degree) WHERE {\n"+
    "  {SELECT (?s AS ?cent) (COUNT(*) AS ?outdegree)\n"+ 
    "    { ?s ?p ?o }\n"+
    "    GROUP BY ?s\n"+
    "    ORDER BY DESC(?outdegree)\n"+
    "  }\n"+
    "  {SELECT (?o AS ?cent) (COUNT(*) AS ?indegree)\n"+ 
    "    { ?s ?p ?o }\n"+
    "    GROUP BY ?o\n"+
    "    ORDER BY DESC(?indegree)\n"+
    "  }\n"+
    "}"
);

@Test
public void test()
{
    model.write(System.out, "TTL");
    System.out.println();

    System.out.println(alldeg);

    final QueryExecution exec0 = QueryExecutionFactory.create(indeg, model);
    ResultSetFormatter.out(exec0.execSelect(), indeg);
    exec0.close();

    final QueryExecution exec1 = QueryExecutionFactory.create(outdeg, model);
    ResultSetFormatter.out(exec1.execSelect(), outdeg);
    exec1.close();

    final QueryExecution exec2 = QueryExecutionFactory.create(alldeg, model);
    ResultSetFormatter.out(exec2.execSelect(), alldeg);
    exec2.close();
}
+3

Source: https://habr.com/ru/post/1544963/


All Articles