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 ...
随机推荐
- RookeyFrame 字典 新增和绑定
原文:https://www.cnblogs.com/rookey/p/10856657.html 注意: 数据字典 -> 新增 把“是否生效”勾上 是否生效都要勾上哦 !!! 应该自动勾上才对 ...
- sudo与用户权限
sudo,以root的身份另起新进程 注意:cd是shell内置的,不会另起新进程,故sudo cd会提示找不到命令 sudo使用当前用户密码,su使用切换用户的密码,默认切换为root sudo通常 ...
- 2D到3D视频转换 三维重建
2D到3D视频转换(也称为2D到立体3D转换和立体转换)是将2D(“平面”)胶片转换为3D形式的过程,几乎在所有情况下都是立体声,因此它是创建图像的过程.每个眼睛来自一个2D图像. 内容 1概述 1. ...
- CSP-S 复赛之前的任务计划
一. 最短路算法复习 ★1.Dijkstra: 2. SPFA: 3. Floyd: 二. DP 复习 ★1.背包问题: 2.区间 DP: 3.状压 DP: 三. 数据结构 ★1. 线段树: 2. 树 ...
- computed的用法
其实在摸板中也是可以做简单的计算的,但是会看起来会很乱 ,可以用computed来做计算 <!DOCTYPE html> <html lang="en"> ...
- mnist卷积网络实现
加载MNIST数据 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_se ...
- Control.ImeMode属性简释
在WINFORM中,我们经常遇到如下问题.文本输入框中输入法有时候需要被禁用,或者某些时候全半角输入自动转换.查阅相关资料,现小结如下. (一)Control.ImeMode 属性:获取或设置控件的输 ...
- Java实现视频网站的视频上传、视频转码、及视频播放功能(ffmpeg)
视频网站中提供的在线视频播放功能,播放的都是FLV格式的文件,它是Flash动画文件,可通过Flash制作的播放器来播放该文件.项目中用制作的player.swf播放器. 多媒体视频处理工具FFmpe ...
- 第2课第2节_Java面向对象编程_封装性_P【学习笔记】
摘要:韦东山android视频学习笔记 面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...
- oracle导入提示“IMP-00010:不是有效的导出文件,头部验证失败”的解决方案
这是由于导出的dmp文件与导入的数据库的版本不同造成的用Notepad++查看了dmp文件,在头部具修改成你将导入目标数据库的版本号以下对应的版本号: 11g R2:V11.02.00 11g R1: ...