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 ...
随机推荐
- IDEA设置提示生成序列化ID
背景: 实现Serializable接口的类,没有提示生成序列化ID 解决问题: 1.FIle->Settings->Editor->inspections 2.点击java-> ...
- Phoenix 简单介绍
转载自:https://blog.csdn.net/carolzhang8406/article/details/79455684 1. Phoenix定义 Phoenix最早是saleforce的一 ...
- leetcode 63 简单题
题目很水... 直接放代码了 int uniquePathsWithObstacles(int** obstacleGrid, int obstacleGridRowSize, int obstacl ...
- Comet OJ - Contest #9 & X Round 3题解
传送门 \(A\) 咕咕 typedef long long ll; int a1,a2,n,d;ll res; int main(){ scanf("%d%d%d",&a ...
- Sublime Text 3 import Anaconda 无法正常补全模块名解决办法
Sublime Text 3 Anaconda配置 在安装Sublime Text3之后我们总会安装一些插件,比如Python的Anaconda自动补全插件.但是,装好之后发现import 时无法像别 ...
- CF1207题解
D 全排列减去坏序列 坏序列分三种,容斥一下就好了 E 比较有意思 \(A=_{i=1}^{100}\{i\},B=_{i=1}^{100}\{i\cdot 2^7\}\),所以\(A_i~xor~ ...
- 深入浅出MYSQL数据库—思维导图[附下载链接]
源文件下载地址:https://github.com/JluTiger/schoolRecruit2020
- elasticsearch routing
当索引一个文档的时候,文档会被存储到一个主分片中. Elasticsearch 如何知道一个文档应该存放到哪个分片中呢?当我们创建文档时,它如何决定这个文档应当被存储在分片 1 还是分片 2 中呢?首 ...
- 生产者消费者模型Java实现
生产者消费者模型 生产者消费者模型可以描述为: ①生产者持续生产,直到仓库放满产品,则停止生产进入等待状态:仓库不满后继续生产: ②消费者持续消费,直到仓库空,则停止消费进入等待状态:仓库不空后,继续 ...
- linux下anaconda使用教程
安装Anaconda.在命令行输入,下载anaconda.wget https://repo.continuum.io/archive/Anaconda3-5.0.1-Linux-x86_64.sh. ...