{"id":8234,"date":"2021-03-13T21:54:57","date_gmt":"2021-03-14T05:54:57","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8234"},"modified":"2021-03-13T22:11:38","modified_gmt":"2021-03-14T06:11:38","slug":"leetcode-1791-find-center-of-star-graph","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-1791-find-center-of-star-graph\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1791. Find Center of Star Graph"},"content":{"rendered":"\n<p>There is an undirected&nbsp;<strong>star<\/strong>&nbsp;graph consisting of&nbsp;<code>n<\/code>&nbsp;nodes labeled from&nbsp;<code>1<\/code>&nbsp;to&nbsp;<code>n<\/code>. A star graph is a graph where there is one&nbsp;<strong>center<\/strong>&nbsp;node and&nbsp;<strong>exactly<\/strong>&nbsp;<code>n - 1<\/code>&nbsp;edges that connect the center node with every other node.<\/p>\n\n\n\n<p>You are given a 2D integer array&nbsp;<code>edges<\/code>&nbsp;where each&nbsp;<code>edges[i] = [u<sub>i<\/sub>, v<sub>i<\/sub>]<\/code>&nbsp;indicates that there is an edge between the nodes&nbsp;<code>u<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>v<sub>i<\/sub><\/code>. Return the center of the given star graph.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><img decoding=\"async\" alt=\"\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/02\/24\/star_graph.png\"><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> edges = [[1,2],[2,3],[4,2]]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> As shown in the figure above, node 2 is connected to every other node, so 2 is the center.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> edges = [[1,2],[5,1],[1,3],[1,4]]\n<strong>Output:<\/strong> 1\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>edges.length == n - 1<\/code><\/li><li><code>edges[i].length == 2<\/code><\/li><li><code>1 &lt;= u<sub>i,<\/sub>&nbsp;v<sub>i<\/sub>&nbsp;&lt;= n<\/code><\/li><li><code>u<sub>i<\/sub>&nbsp;!= v<sub>i<\/sub><\/code><\/li><li>The given&nbsp;<code>edges<\/code>&nbsp;represent a valid star graph.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Graph \/ Hashtable<\/strong><\/h2>\n\n\n\n<p>Count the degree of each node, return the one with n-1 degrees.<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int findCenter(vector<vector<int>>& edges) {\n    unordered_map<int, int> degrees;\n    for (const auto& e : edges) {\n      ++degrees[e[0]];\n      ++degrees[e[1]];\n    }\n    const int n = degrees.size();\n    for (const auto& [id, d] : degrees)\n      if (d == n - 1) return id;\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p>Since the center node must appear in each edge, we just need to find the mode of edges[0] + edges[1]<\/p>\n\n\n\n<p>Time complexity: O(1)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n# Author: Huahua\nclass Solution:\n  def findCenter(self, e):\n    return mode(e[0] + e[1])\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There is an undirected&nbsp;star&nbsp;graph consisting of&nbsp;n&nbsp;nodes labeled from&nbsp;1&nbsp;to&nbsp;n. A star graph is a graph where there is one&nbsp;center&nbsp;node and&nbsp;exactly&nbsp;n &#8211; 1&nbsp;edges that connect the center&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,82,177],"class_list":["post-8234","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-hashtable","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8234"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8234\/revisions"}],"predecessor-version":[{"id":8237,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8234\/revisions\/8237"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}