> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kinetica.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SOLVE_GRAPH

Solves an existing [graph](https://docs.kinetica.com/content/graph_solver/network_graph_solver.md)
using one of the supported [solver types](https://docs.kinetica.com/content/graph_solver/network_graph_solver.md#solvers).  The
`SOLVE GRAPH` function can be called either within a `SELECT` statement as a
table function or within an `EXECUTE FUNCTION` call.

```sql Table Function Syntax theme={null}
SELECT * FROM TABLE
(
	SOLVE_GRAPH
	(
		GRAPH => '[<schema name>.]<graph name>',
		SOLVER_TYPE => '<solver type>',
		SOURCE_NODES => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,
		[DESTINATION_NODES => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,]
		[WEIGHTS_ON_EDGES => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,]
		[RESTRICTIONS => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,]
		[OPTIONS => KV_PAIRS('<option name>' = '<option value>'[,...])]
	)
)
```

```sql EXECUTE FUNCTION Syntax theme={null}
EXECUTE FUNCTION SOLVE_GRAPH
(
	GRAPH => '[<schema name>.]<graph name>',
	SOLVER_TYPE => '<solver type>',
	SOURCE_NODES => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,
	SOLUTION_TABLE => '[<schema name>.]<table name>',
	[DESTINATION_NODES => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,]
	[WEIGHTS_ON_EDGES => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,]
	[RESTRICTIONS => <INPUT_TABLE(<table/query>) | INPUT_TABLES((<table/query>)[,...])>,]
	[OPTIONS => KV_PAIRS('<option name>' = '<option value>'[,...])
)
```

## Parameters

> [accordion] GRAPH
>
> Name of the *graph* to solve

> [accordion] SOLVER\_TYPE
>
> The type of solver to use for the operation.
>
> <table>
>   <thead>
>     <tr>
>       <th>Solver</th>
>       <th>Description</th>
>       <th>CPU Parallel</th>
>     </tr>
>   </thead>
>
>   <tbody>
>     <tr>
>       <td>`ALLPATHS`</td>
>       <td>Determines all reasonable paths between a source and destination pair.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`BACKHAUL_ROUTING`</td>
>       <td>Determines the optimal routes between remote asset *nodes* and fixed asset nodes.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`CENTRALITY`</td>
>       <td>Calculates the degree of a *node* to depict how many pairs of individuals that would have to go through the *node* to reach one another in the minimum number of hops. Also known as betweenness.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`CLOSENESS`</td>
>       <td>Calculates the centrality closeness score per *node* as the sum of the inverse shortest path costs to all *nodes* in the graph.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`INVERSE_SHORTEST_PATH`</td>
>       <td>Determines the shortest path downstream using multiple technician routing.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`MULTIPLE_ROUTING`</td>
>       <td>Calculates the shortest possible route between the *nodes* and returns to the origin *node* -- also known as the traveling salesman.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`PAGE_RANK`</td>
>       <td>Calculates how connected the *nodes* are and determines which *nodes* are the most important.  Weights are not required.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`PROBABILITY_RANK`</td>
>       <td>Calculates the probability of a *node* being connected to another *node* using hidden Markov chains.</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`SHORTEST_PATH`</td>
>       <td>Determines the shortest path upstream between given source(s) and destination(s).</td>
>       <td>X</td>
>     </tr>
>
>     <tr>
>       <td>`STATS_ALL`</td>
>       <td>Calculates graph statistics such as graph diameter, longest pairs, vertex valences, topology numbers, average and max cluster sizes, etc.</td>
>       <td>X</td>
>     </tr>
>   </tbody>
> </table>

> [accordion] SOURCE\_NODES
>
> The *node(s)* used as the origin point(s) for the solution specified using the SQL `INPUT_TABLE` or
> `INPUT_TABLES` function.
>
> > [!NOTE]
> >
> > The input can be either the name of a table with appropriately named columns; or a query, to use
> > specific columns or other SQL features).

> [accordion] SOLUTION\_TABLE
>
> Only applicable when using `EXECUTE FUNCTION` syntax. Name of the table to store the solution
> in `[schema_name.]table_name` format, using standard [name resolution rules](https://docs.kinetica.com/content/sql/naming.md#sql-name-resolution)
> and meeting [table naming criteria](https://docs.kinetica.com/content/sql/naming.md#sql-naming-criteria)

> [accordion] DESTINATION\_NODES
>
> The *node(s)* used as the destination point(s) for the solution specified using the SQL `INPUT_TABLE` or
> `INPUT_TABLES` function.
>
> > [!NOTE]
> >
> > The input can be either the name of a table with appropriately named columns; or a query, to use
> > specific columns or other SQL features).

> [accordion] WEIGHTS\_ON\_EDGES
>
> Additional *weights* to apply to the *edges* of an existing graph specified using the SQL `INPUT_TABLE` or
> `INPUT_TABLES` function; review [Components and Identifiers](https://docs.kinetica.com/content/graph_solver/network_graph_solver.md#identifiers) and [Identifier Combinations](https://docs.kinetica.com/content/graph_solver/network_graph_solver.md#id-combos) for more information.
>
> > [!NOTE]
> >
> > The input can be either the name of a table with appropriately named columns; or a query, to use
> > specific columns or other SQL features).

> [accordion] RESTRICTIONS
>
> Additional *restrictions* to apply to the *nodes*/*edges* of an existing graph specified using the SQL
> `INPUT_TABLE` or `INPUT_TABLES` function; review [Components and Identifiers](https://docs.kinetica.com/content/graph_solver/network_graph_solver.md#identifiers) and [Identifier Combinations](https://docs.kinetica.com/content/graph_solver/network_graph_solver.md#id-combos) for more
> information.
>
> > [!NOTE]
> >
> > The input can be either the name of a table with appropriately named columns; or a query, to use
> > specific columns or other SQL features).

> [accordion] OPTIONS
>
> Optional indicator that a comma-delimited list of connection option/value assignments will follow.
>
> <table>
>   <thead>
>     <tr>
>       <th>Option</th>
>       <th>Description</th>
>     </tr>
>   </thead>
>
>   <tbody>
>     <tr>
>       <td>`astar_radius`</td>
>       <td>For `SHORTEST_PATH`, `INVERSE_SHORTEST_PATH`, `ALLPATHS`, and `MULTIPLE_ROUTING` solvers, when `solve_heuristic` is `astar`. The shortest path traversal front includes nodes only within this radius (kilometers) as it moves towards the target location. Default is `70`.</td>
>     </tr>
>
>     <tr>
>       <td>`convergence_limit`</td>
>       <td>For `PAGE_RANK` solver only; maximum percent relative threshold on the pagerank scores of each *node* between consecutive iterations to satisfy convergence. Default value is 1%.</td>
>     </tr>
>
>     <tr>
>       <td>`intersection_penalty`</td>
>       <td>This will add an additional weight over the edges labeled as `intersection` if the `add_turn` option was invoked during graph creation. The default value is `0.0`.</td>
>     </tr>
>
>     <tr>
>       <td>`left_turn_penalty`</td>
>       <td>This will add an additional weight over the edges labeled as `left_turn` if the `add_turn` option was invoked during graph creation. The default value is `0.0`.</td>
>     </tr>
>
>     <tr>
>       <td>`max_iterations`</td>
>       <td>For `PAGE_RANK` solver only; maximum number of pagerank iterations for satisfying convergence. Default value is 100.</td>
>     </tr>
>
>     <tr>
>       <td>`max_num_combinations`</td>
>       <td>For `MULTIPLE_ROUTING` solver only. Sets the cap on the combinatorial sequence generated. If the default value (`2000000`) is overridden, it potentially speeds up the solver.</td>
>     </tr>
>
>     <tr>
>       <td>`max_runs`</td>
>       <td>For `CENTRALITY` solver only. Sets the maximum number of shortest path runs--maximum possible value is the number of *nodes* in the graph. Default of `0` enables this value to be auto-computed by the solver.</td>
>     </tr>
>
>     <tr>
>       <td>`max_solution_radius`</td>
>       <td>For `SHORTEST_PATH`, `INVERSE_SHORTEST_PATH`, & `ALLPATHS` solvers only.  Sets the maximum solution cost radius, which ignores the `DESTINATION_NODES` list and instead outputs the nodes within the radius sorted by ascending cost.  If set to `0.0` (the default), the setting is ignored.</td>
>     </tr>
>
>     <tr>
>       <td>`max_solution_targets`</td>
>       <td>For `SHORTEST_PATH`, `INVERSE_SHORTEST_PATH`, & `ALLPATHS` solvers only.  Sets the maximum number of solution targets, which ignores the `DESTINATION_NODES` list and instead outputs no more than *n* number of nodes sorted by ascending cost where *n* is equal to the setting value. If set to `0` (the default), the setting is ignored.</td>
>     </tr>
>
>     <tr>
>       <td>`min_solution_radius`</td>
>       <td>For `SHORTEST_PATH`, `INVERSE_SHORTEST_PATH`, & `ALLPATHS` solvers only.  Applicable when `max_solution_radius` is set.  Sets the minimum solution cost radius, which ignores the `DESTINATION_NODES` list and instead outputs the nodes within the radius sorted by ascending cost. If set to `0.0` (the default), the setting is ignored.</td>
>     </tr>
>
>     <tr>
>       <td>`num_best_paths`</td>
>       <td>For `MULTIPLE_ROUTING` solver only. Sets the number of shortest paths computed from each node. This is the heuristic criterion. If set to `0` (the default), the number will be determined automatically by the solver. Users may want to override this parameter to speed up the solver.</td>
>     </tr>
>
>     <tr>
>       <td>`output_clusters`</td>
>       <td>For `STATS_ALL` solver only; the cluster index for each *node* will be inserted as an additional column in the output.</td>
>     </tr>
>
>     <tr>
>       <td>`output_edge_path`</td>
>       <td>If set to `true`, concatenated edge IDs will be added as an `EDGE_PATH` column to the solution table for each source and target pair in the `SHORTEST_PATH` solves. The default value is `false`</td>
>     </tr>
>
>     <tr>
>       <td>`output_wkt_path`</td>
>       <td>If set to `true`, WKT line segments will be added as a `WKTROUTE` column to the solution table for each source and target pair in `SHORTEST_PATH` solves. The default value is `false`.</td>
>     </tr>
>
>     <tr>
>       <td>`right_turn_penalty`</td>
>       <td>This will add an additional weight over the edges labeled as `right_turn` if the `add_turn` option was invoked during graph creation. The default value is `0.0`.</td>
>     </tr>
>
>     <tr>
>       <td>`server_id`</td>
>       <td>Indicates which graph server(s) to send the request to. Default is to send to the server, amongst those containing the corresponding graph, that has the most computational bandwidth. For `SHORTEST_PATH` solver, the input is split among the server containing the corresponding graph.</td>
>     </tr>
>
>     <tr>
>       <td>`sharp_turn_penalty`</td>
>       <td>This will add an additional weight over the edges labeled as `sharp_turn` or `u_turn` if the `add_turn` option was invoked during graph creation. The default value is `0.0`.</td>
>     </tr>
>
>     <tr>
>       <td>`solve_heuristic`</td>
>       <td>The heuristic search criterion to use, only for geo graphs and `SHORTEST_PATH` solves towards a single target.  Specify `astar` to use an *A-STAR* heuristic.  The default value is `none`.</td>
>     </tr>
>
>     <tr>
>       <td>`uniform_weights`</td>
>       <td>When specified, assigns the given value to all the edges in the graph. Note that weights provided using `weights_on_edges` will override this value.</td>
>     </tr>
>   </tbody>
> </table>

## Examples

Using the `EXECUTE FUNCTION` syntax necessitates the `SOLUTION_TABLE`
parameter to store the solve results, where using the table function syntax will
return the results directly.

```sql Table Function Example theme={null}
SELECT * FROM TABLE
(
	SOLVE_GRAPH
	(
		GRAPH => 'sample_graph',
		SOLVER_TYPE => 'SHORTEST_PATH',
		SOURCE_NODES => INPUT_TABLE(SELECT ST_GEOMFROMTEXT('POINT(0 0)') AS WKTPOINT),
		DESTINATION_NODES => INPUT_TABLE(SELECT ST_GEOMFROMTEXT('POINT(-5 -5)') AS WKTPOINT),
		OPTIONS => KV_PAIRS(output_edge_path = 'true')
	)
)
```

```sql EXECUTE FUNCTION Example theme={null}
EXECUTE FUNCTION SOLVE_GRAPH
(
	GRAPH => 'sample_graph',
	SOLVER_TYPE => 'SHORTEST_PATH',
	SOURCE_NODES => INPUT_TABLE(SELECT ST_GEOMFROMTEXT('POINT(0 0)') AS WKTPOINT),
	DESTINATION_NODES => INPUT_TABLE(SELECT ST_GEOMFROMTEXT('POINT(-5 -5)') AS WKTPOINT),
	SOLUTION_TABLE => 'sample_graph_solved_shortest_path',
	OPTIONS => KV_PAIRS(output_edge_path = 'true')
)
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.