Find minimum number of people to reach to spread a message across all people in twitter
Considering that I'ld would like to spread a promotion message across all people in twitter. Assuming the ideal case, if a person tweets a message, then every follower will re-tweet the message.
You need to find the minimum number of people to reach out (for example, who doesn't follow anyone etc) so that your promotion message is spread out across entire network in twitter.
Also, we need to consider loops like, if A follows B, B follows C, C follows D, D follows A (A -> B -> C -> D -> A) then reaching only one of them is sufficient to spread your message.
Input: A 2x2 matrix like below. In this case, a follows b, b follows c, c follows a.
a b c
a 1 1 0
b 0 1 1
c 1 0 1
Output: List of people to be reached to spread out message across everyone in the network.
F家
解法:
This is a very interesting graph problem, here is what I would do:
step 1. Build a directed graph based on the input people (nodes) and their relationship (edges).
step 2. Find strongly connected components (SCCs) in the graph. Let's use the wikipedia's graph example, in that case, there are 3
SCCs: (a, b, e)
, (c, d, h)
and (f, g)
. There are two famous algorithms for getting the SCCs: Kosaraju's algorithm and Tarjan's algorithm.
step 3. Pick one of the nodes from the SCCs we get: a, c, f
, now these 3
nodes form a DAG, we just need to do topological sort for them, eventually a
is the root node in the path (or stack), and we can let a
spread the message and guarantee all other people will get it.
Sometimes, there could be several topological paths, and the root nodes of those paths will be the minimum people to reach out to spread the message.
Create a directed graph which captures followee -> follower relationship
Now create the topological sort for the entire graph
For each unvisited node in the topological sort result, add it to the final result and then visit all the nodes in that tree
The reason this works is, in the topological sort order the node that appears first is the left most node in the given connected component. So you would use that to traverse the curr node and all its children node.
def min_people(num_people, follows):
from collections import defaultdict # in this graph we will store
# followee -> follower relation
graph = defaultdict(set) # a follows b
for a, b in follows:
graph[b].add(a) def topo(node, graph, visited, result):
visited.add(node)
for nei in graph[node]:
if nei not in visited:
topo(nei, graph, visited, result)
result.append(node) visited = set([])
result = []
for i in range(num_people):
if i not in visited:
topo(i, graph, visited, result)
result = list(reversed(result)) def visit(node, visited, graph):
visited.add(node)
for nei in graph[node]:
if nei not in visited:
visit(nei, visited, graph) visited = set([])
start_with = []
for r in result:
if r not in visited:
start_with.append(r)
visit(r, visited, graph) return start_with
类似题目:
[LeetCode] 323. Number of Connected Components in an Undirected Graph 无向图中的连通区域的个数
Find minimum number of people to reach to spread a message across all people in twitter的更多相关文章
- [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [LeetCode] 452 Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Reorder array to construct the minimum number
Construct minimum number by reordering a given non-negative integer array. Arrange them such that th ...
- Leetcode: Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Lintcode: Interval Minimum Number
Given an integer array (index from 0 to n-1, where n is the size of this array), and an query list. ...
- 452. Minimum Number of Arrows to Burst Balloons——排序+贪心算法
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Codeforces 279D The Minimum Number of Variables 状压dp
The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...
- [Swift]LeetCode452. 用最少数量的箭引爆气球 | Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- [Swift]LeetCode995. K 连续位的最小翻转次数 | Minimum Number of K Consecutive Bit Flips
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray o ...
随机推荐
- SpringMVC的文件上传与下载
1. 单文件上传 配置jsp页面 <%@ page contentType="text/html;charset=UTF-8" language="java&quo ...
- DVWA-文件包含漏洞
本周学习内容: 1.学习web安全深度剖析: 2.学习安全视频: 3.学习乌云漏洞: 4.学习W3School中PHP: 实验内容: 进行DVWA文件包含实验 实验步骤: Low 1.打开DVWA,进 ...
- bzoj 5299: [Cqoi2018]解锁屏幕 状压dp+二进制
比较简单的状压 dp,令 $f[S][i]$ 表示已经经过的点集为 $S$,且最后一个访问的位置为 $i$ 的方案数. 然后随便转移一下就可以了,可以用 $lowbit$ 来优化一下枚举. code: ...
- 【转】跟我一起写 Makefile
概述—— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉得要作一个好的和professional的程序员,makefi ...
- 洛谷 P2251 质量检测 题解
P2251 质量检测 题目背景 无 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, ...
- Codeforces 1172F Nauuo and Bug [线段树]
Codeforces 思路 定义\(f_{l,r}(x)\)表示数\(x\)从\(l\)进去\(r\)出来的时候会变成什么样子.容易发现这个函数是个分段函数,每一段都是斜率为1的一次函数,并且段数就是 ...
- Semantic Segmentation on Remotely Sensed Images Using an Enhanced Global Convolutional Network with Channel Attention and Domain Specific Transfer Learning
创新点: 1.在GCN(global convolutional network)基础上,把他的backbone替换成更多层的,使其适应中分辨率影像,resnet50,101,152 2.利用 cha ...
- 怎么判断是qq浏览器还是uc浏览器?
这里我画红框的是不正确的,最好的办法就是打印出navigator.userAgent出来.uc浏览器检验是正确的.
- fluent-动网格-动态层
模型算例来源:http://blog.sina.com.cn/s/blog_599d8faa0100w4uj.html 原视频下载地址:http://yunpan.cn/cujM2FxLuGdLK ...
- URLEncoder.encode 使用心得
1.解决接口GET请求中:参数包含中文问题? 通过URLEncoder.encode 可以解决.我此次使用的utf-8编码,所以对中文进行urlencode 编码 而在服务器短 使用的tomcat ...