|
| 1 | +--- |
| 2 | +title: graph-mark-components operator (Preview) |
| 3 | +description: Learn how to use the graph-mark-components operator to find and mark all connected components of a graph. |
| 4 | +ms.reviewer: royo |
| 5 | +ms.topic: reference |
| 6 | +ms.date: 11/06/2024 |
| 7 | +--- |
| 8 | +# graph-mark-components operator (Preview) |
| 9 | + |
| 10 | +> [!INCLUDE [applies](../includes/applies-to-version/applies.md)] [!INCLUDE [fabric](../includes/applies-to-version/fabric.md)] [!INCLUDE [azure-data-explorer](../includes/applies-to-version/azure-data-explorer.md)] [!INCLUDE [monitor](../includes/applies-to-version/monitor.md)] [!INCLUDE [sentinel](../includes/applies-to-version/sentinel.md)] |
| 11 | +
|
| 12 | +The `graph-mark-components` operator finds all connected components of a graph and marks each node with a component identifier. |
| 13 | + |
| 14 | +> [!NOTE] |
| 15 | +> This operator is used in conjunction with the [make-graph operator](make-graph-operator.md). |
| 16 | +
|
| 17 | +## Syntax |
| 18 | + |
| 19 | +*G* `|` `graph-mark-components` [`kind` `=` *Kind*] [`with_component_id` `=` *ComponentId*] |
| 20 | + |
| 21 | +## Parameters |
| 22 | + |
| 23 | +| Name | Type | Required | Description | |
| 24 | +|--|--|--|--| |
| 25 | +| *G* | string | :heavy_check_mark: | The graph source. | |
| 26 | +| *Kind* | string | | The connected component kind, either `weak` (default) or `strong`. A weak component is a set of nodes connected by a path, ignoring the direction of edges. A strong component is a set of nodes connected in both directions, considering the edges' directions. | |
| 27 | +| *ComponentId* | string | | The property name that denotes the component identifier. The default property name is `ComponentId`. | |
| 28 | + |
| 29 | +## Returns |
| 30 | + |
| 31 | +The `graph-mark-components` operator returns a *graph* result, where each node has a component identifier in the *ComponentId* property. The identifier is a zero-based consecutive index of the components. Each component index is chosen arbitrarily and might not be consistent across runs. |
| 32 | + |
| 33 | +## Examples |
| 34 | + |
| 35 | +### Find families by their relationships |
| 36 | + |
| 37 | +The following example creates a graph from a set of child-parent pairs and identifies connected components using a `family` identifier. |
| 38 | + |
| 39 | +:::moniker range="azure-data-explorer" |
| 40 | +> [!div class="nextstepaction"] |
| 41 | +> <a href="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAA2WQ24rCMBCG7%2FMUQ65caF5AccGzILIPsCwyNrENzaFMw4qwD79pNLUiuUj%2Bb77JxW9UgFWtjfy6wBwkhnjORk3Knk27QNpVBbRIyoVH%2FAD2DQyAL4wuFS%2BAL%2F05XomtkLzpWR6%2B0jX%2BDnBHqnozMxyLe39Fkm%2FqE4%2FlTXryLaFrMsuBH9EYTzdejMyDpjhjPzNguQj2BxYbJSrCtoZUBQjx%2BagBrjrUJ%2BelOmk5d2hV9JMqLFIjSm9b76LY3c0hRz12fEGrzW1YCV6kxqH%2FsPsH1QoVAI8BAAA%3D" target="_blank">Run the query</a> |
| 42 | +::: moniker-end |
| 43 | + |
| 44 | +```kusto |
| 45 | +let ChildOf = datatable(child:string, parent:string) |
| 46 | +[ |
| 47 | + "Alice", "Bob", |
| 48 | + "Carol", "Alice", |
| 49 | + "Carol", "Dave", |
| 50 | + "Greg", "Alice", |
| 51 | + "Greg", "Dave", |
| 52 | + "Howard", "Alice", |
| 53 | + "Howard", "Dave", |
| 54 | + "Eve", "Frank", |
| 55 | + "Frank", "Mallory", |
| 56 | + "Eve", "Kirk", |
| 57 | +]; |
| 58 | +ChildOf |
| 59 | +| make-graph child --> parent with_node_id=name |
| 60 | +| graph-mark-components with_component_id = family |
| 61 | +| graph-to-table nodes |
| 62 | +``` |
| 63 | + |
| 64 | +**Output** |
| 65 | + |
| 66 | +|name|family| |
| 67 | +|---|---| |
| 68 | +|Alice|0| |
| 69 | +|Bob|0| |
| 70 | +|Carol|0| |
| 71 | +|Dave|0| |
| 72 | +|Greg|0| |
| 73 | +|Howard|0| |
| 74 | +|Eve|1| |
| 75 | +|Frank|1| |
| 76 | +|Mallory|1| |
| 77 | +|Kirk|1| |
| 78 | + |
| 79 | +### Find a greatest common ancestor for each family |
| 80 | + |
| 81 | +The following example uses the connected component `family` identifier and the `graph-match` operator to identify the greatest ancestor of each family in a set of child-parent data. |
| 82 | + |
| 83 | +:::moniker range="azure-data-explorer" |
| 84 | +> [!div class="nextstepaction"] |
| 85 | +> <a href="https://dataexplorer.azure.com/clusters/help/databases/Samples?query=H4sIAAAAAAAAA2WQy07DQAxF9%2FkKK6sEJZFYsAG1EpSXhBAfUFWVO%2BMmQ%2BYROQMliI9nkiYlqJqFx9fHvpY1eVhVSsu3PSxAog9vpykRvXbdela2zKBBJuvHNIVoDRFAfKuVoDiD%2BM7tQhi0FbLTvTYV%2F6v3%2BHkSn5jKM3IS5%2BCzOyDLM%2FRPnsMPwzd%2BZLT1pE1J%2FIpaO%2B7ibEa%2BKA61aHMD0XSI6AcM1pSXjE0Fwykgz5fjGeCgfLW1TtJWyYVFQ4Ef0Nwg17lwpnE2gO2RPOUBDzfeo1G6m7V4UUEiqRVkJVqf5mtx3OPisiiuNvkyQSuo9Y7TsHbD7p2Eh943TJtKRZ9noJUlLPvCOKMYYja6zhtOe7QfJuytvgmSkiwxeuVsmw0Oad%2FBpcGvBJmx22qypa%2BS0SfNjtCuGw1%2BAcTWyMtPAgAA" target="_blank">Run the query</a> |
| 86 | +::: moniker-end |
| 87 | + |
| 88 | +```kusto |
| 89 | +let ChildOf = datatable(child:string, parent:string) |
| 90 | +[ |
| 91 | + "Alice", "Bob", |
| 92 | + "Carol", "Alice", |
| 93 | + "Carol", "Dave", |
| 94 | + "Greg", "Alice", |
| 95 | + "Greg", "Dave", |
| 96 | + "Howard", "Alice", |
| 97 | + "Howard", "Dave", |
| 98 | + "Eve", "Frank", |
| 99 | + "Frank", "Mallory", |
| 100 | + "Eve", "Kirk", |
| 101 | +]; |
| 102 | +ChildOf |
| 103 | +| make-graph child --> parent with_node_id=name |
| 104 | +| graph-mark-components with_component_id = family |
| 105 | +| graph-match (descendant)-[childOf*1..5]->(ancestor) |
| 106 | + project name = ancestor.name, lineage = childOf.child, family = ancestor.family |
| 107 | +| summarize (generations, name) = argmax(array_length(lineage),name) by family |
| 108 | +``` |
| 109 | + |
| 110 | +**Output** |
| 111 | + |
| 112 | +|family|generations|name| |
| 113 | +|---|---|---| |
| 114 | +|1|2|Mallory| |
| 115 | +|0|2|Bob| |
| 116 | + |
| 117 | +## Related content |
| 118 | + |
| 119 | +* [Best practices for Kusto Query Language (KQL) graph semantics](graph-best-practices.md) |
| 120 | +* [Graph operators](graph-operators.md) |
| 121 | +* [make-graph operator](make-graph-operator.md) |
| 122 | +* [graph-match operator](graph-match-operator.md) |
| 123 | +* [graph-to-table operator](graph-to-table-operator.md) |
| 124 | + |
0 commit comments