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 ...
随机推荐
- Acwing P298 围栏
Analysis ①首先将所有粉刷匠,按照必须刷的小木块Si从小到大排序. 上面这个操作为了保证我们可以顺序处理. ②我们可以设f[i][j]表示为,前i个粉刷匠,刷了前i个木块.可以有些木块选择不刷 ...
- 对日开发中 PG , PL , SE , PM 是什么
PG(ProGramer)指程序员. 这类人才在企业中所占数量最多,通常占到整个项目员工数的70%,也是企业中最紧缺的一类职位,一般为具有专业知识的软件工程技术人员. PL(project leade ...
- Ubuntu 下面手动安装 Redis
1.下载 wget http://download.redis.io/releases/redis-2.8.17.tar.gz .tar.gz cd redis- make 2.复制文件到bin目录 ...
- 任意模数FFT
任意模数FFT 这是一个神奇的魔法,但是和往常一样,在这之前,先 \(\texttt{orz}\ \color{orange}{\texttt{matthew99}}\) 问题描述 给定 2 个多项式 ...
- 实现本地des和aes 解密的工具
<?php $raw = file_get_contents('php://input'); if(!empty($raw)) { parse_str($raw);//解析到当前作用域 if ( ...
- python3 ssl导入失败
make LibreSSL 2.6.4 and earlier do not provide the necessary APIs /root/Python-3.7.0/build/lib.linux ...
- useReducer介绍和简单使用(六)
上节课学习了useContext函数,那这节课开始学习一下useReducer,因为他们两个很像,并且合作可以完成类似的Redux库的操作.在开发中使用useReducer可以让代码具有更好的可读性和 ...
- JVM Java字节码方法表与属性
方法表 1.methods_count method_info,前三个字段和field_info一样 2.方法的属性结构 方法中的每个属性都是一个attribut_info结构 JVM定义了部分at ...
- 阿里云ECS服务器环境搭建(1) —— ubuntu 16.04 图形界面的安装
阿里云ECS服务器环境搭建(1) —— ubuntu 16.04 图形界面的安装1. 背景在我们购买阿里云ECS服务器之后,默认的系统环境是很干净的,我购买的是ubuntu16.04,远程登录进入之后 ...
- mediacoder固定质量CRF
视频编码:crf 与 bitrate 对照表 CRF(constant rate factor)就是x264/x265下压制视频的一种恒定量化值的编码方式,码率不恒定.其实就相当于vbr1pass.采 ...