<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SCC Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/scc/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/scc/</link>
	<description></description>
	<lastBuildDate>Fri, 31 Aug 2018 19:37:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>SCC Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/scc/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 547. Friend Circles</title>
		<link>https://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/</link>
					<comments>https://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 22 Sep 2017 04:29:41 +0000</pubDate>
				<category><![CDATA[Graph]]></category>
		<category><![CDATA[connected components]]></category>
		<category><![CDATA[DFS]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[SCC]]></category>
		<category><![CDATA[union find]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=382</guid>

					<description><![CDATA[<p>Problem: There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/">花花酱 LeetCode 547. Friend Circles</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/HHiHno66j40?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>There are <b>N</b> students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a <b>direct</b> friend of B, and B is a <b>direct</b> friend of C, then A is an <b>indirect</b> friend of C. And we defined a friend circle is a group of students who are direct or indirect friends.</p>
<p>Given a <b>N*N</b> matrix <b>M</b> representing the friend relationship between students in the class. If M[i][j] = 1, then the i<sub>th</sub>and j<sub>th</sub> students are <b>direct</b> friends with each other, otherwise not. And you have to output the total number of friend circles among all the students.</p>
<p><b>Example 1:</b></p><pre class="crayon-plain-tag">Input: 
[[1,1,0],
 [1,1,0],
 [0,0,1]]
Output: 2</pre><p>E<b>xample 2:</b></p><pre class="crayon-plain-tag">Input: 
[[1,1,0],
 [1,1,1],
 [0,1,1]]
Output: 1</pre><p></p>
<ol>
<li>N is in range [1,200].</li>
<li>M[i][i] = 1 for all students.</li>
<li>If M[i][j] = 1, then M[j][i] = 1.</li>
</ol>
<p><strong>Idea:</strong></p>
<p>Find all connected components using DFS</p>
<p><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><br />
<ins class="adsbygoogle" style="display: block; text-align: center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-2404451723245401" data-ad-slot="7983117522"></ins><br />
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script></p>
<h1><strong>Solution: DFS</strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Time complexity: O(n^2)
// Space complexity: O(n)
// Running Time: 25 ms
class Solution {
public:
    int findCircleNum(vector&lt;vector&lt;int&gt;&gt;&amp; M) {
        if (M.empty()) return 0;
        int n = M.size();
        int ans = 0;
        for (int i = 0; i &lt; n; ++i) {
            if (!M[i][i]) continue;
            ++ans;
            dfs(M, i, n);
        }
        return ans;
    }
private:
    void dfs(vector&lt;vector&lt;int&gt;&gt;&amp; M, int curr, int n) {        
        // Visit all friends (neighbors)
        for (int i = 0; i &lt; n; ++i) {
            if (!M[curr][i]) continue;
            M[curr][i] = M[i][curr] = 0;
            dfs(M, i, n);
        }
    }
};</pre><p></div><h2 class="tabtitle">Java</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Time complexity: O(n^2)
// Space complexity: O(n)
// Running Time: 20 ms
class Solution {
    public int findCircleNum(int[][] M) {
        int n = M.length;
        if (n == 0) return 0;
        
        int ans = 0;
        for (int i = 0; i &lt; n; ++i) {
            if (M[i][i] == 0) continue;            
            ++ans;
            dfs(M, i, n);
        }
        return ans;
    }
    
    private void dfs(int[][] M, int curr, int n) {
        for (int i = 0; i &lt; n; ++i) {
            if (M[curr][i] == 0) continue;
            M[curr][i] = M[i][curr] = 0;
            dfs(M, i, n);
        }
    }
}</pre><p></div><h2 class="tabtitle">Python</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">"""
Author: Huahua
Time complexity: O(n^2)
Space complexity: O(n)
Running Time: 162 ms
"""
class Solution(object):
    def findCircleNum(self, M):
        """
        :type M: List[List[int]]
        :rtype: int
        """
        
        def dfs(M, curr, n):
            for i in xrange(n):
                if M[curr][i] == 1:
                    M[curr][i] = M[i][curr] = 0
                    dfs(M, i, n)
        
        n = len(M)
        ans = 0
        for i in xrange(n):
            if M[i][i] == 1:
                ans += 1
                dfs(M, i, n)
        
        return ans</pre><p></div></div></p>
<h1><strong>Solution 2: <a href="https://zxi.mytechroad.com/blog/data-structure/sp1-union-find-set/">Union Find</a></strong></h1>
<p><div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">
</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 19 ms
class UnionFindSet {
public:
    UnionFindSet(int n) {
        parents_ = vector&lt;int&gt;(n + 1, 0);
        ranks_ = vector&lt;int&gt;(n + 1, 0);
        
        for (int i = 0; i &lt; parents_.size(); ++i)
            parents_[i] = i;
    }
    
    bool Union(int u, int v) {
        int pu = Find(u);
        int pv = Find(v);
        if (pu == pv) return false;
        
        if (ranks_[pu] &gt; ranks_[pv]) {
            parents_[pv] = pu;
        } else if (ranks_[pv] &gt; ranks_[pu]) {
            parents_[pu] = pv;
        } else {
            parents_[pu] = pv;
            ++ranks_[pv];
        }
 
        return true;
    }
    
    int Find(int id) {        
        if (id != parents_[id])
            parents_[id] = Find(parents_[id]);        
        return parents_[id];
    }
    
private:
    vector&lt;int&gt; parents_;
    vector&lt;int&gt; ranks_;
};
 
class Solution {
public:
    int findCircleNum(vector&lt;vector&lt;int&gt;&gt;&amp; M) {
        int n = M.size();
        UnionFindSet s(n);
        for (int i = 0; i &lt; n; ++i)
            for (int j = i + 1; j &lt; n; ++j)
                if (M[i][j] == 1) s.Union(i, j);
        
        unordered_set&lt;int&gt; circles;
        for (int i = 0; i &lt; n; ++i)
            circles.insert(s.Find(i));
        
        return circles.size();
    }
};</pre><p></div></div></p>
<p>&nbsp;</p>
<h1><strong>Related Problems</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/searching/leetcode-200-number-of-islands/">[解题报告] LeetCode 200. Number of Islands</a></li>
<li><a href="http://zxi.mytechroad.com/blog/graph/leetcode-695-max-area-of-island/">[解题报告] LeetCode 695. Max Area of Island</a></li>
<li><a href="http://zxi.mytechroad.com/blog/tree/leetcode-684-redundant-connection/">[解题报告] LeetCode 684. Redundant Connection</a></li>
<li><a href="http://zxi.mytechroad.com/blog/graph/leetcode-685-redundant-connection-ii/">[解题报告] LeetCode 685. Redundant Connection II</a></li>
<li><a href="http://zxi.mytechroad.com/blog/hashtable/leetcode-737-sentence-similarity-ii/">[解题报告] LeetCode 737. Sentence Similarity II</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/">花花酱 LeetCode 547. Friend Circles</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/graph/leetcode-547-friend-circles/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
