- chung
Given an adjacency-list representation of a directed graph, how long does it take to compute the out-degree of every vertex? How long does it take to compute the in-degrees
要在有向圖的adjacency-list計算out-degree,從vertex的adjacency-list中遍歷所有相鄰邊(E),即便圖沒有邊仍然需要判斷vertex(V),因此所需時間為
要計算in-degree,必須遍歷每個頂點(V),走完所有的點及邊即可確認所有的in-degree,因此所需時間為
- xian
The transpose of a directed graph is the graph , where . Thus, is with all its edges reversed. Describe efficient algorithms for computing from , for both the adjacency-list and adjacency-matrix representations of . Analyze the running times of your algorithms.
【example】
由下圖可知,只要的 Adjacency-List
遍歷一次,即可找到的 Adjacency-List
,每經過任何一個節點的List時,即在的List加上相對的節點。
Adjacency-List
【蘇都扣】
由下圖可知,我們只需將Adjacency-Matrix
轉置,即可計算出的Adjacency-Matrix
【蘇都扣】
- LXS
The square of a directed graph is the graph such that if and only if contains a path with at most two edges between and . Describe efficient algorithms for computing from for both the adjacency-list and adjacency-matrix epresentations of . Analyze the running times of your algorithms.
The Adjency matrix of the graph
這樣沒算到因為沒有self-loop時,是,,同理
因此再加上,即,加法時間複雜度最高為,整體的時間複雜度為矩陣乘法的時間複雜度,Strassen為例,時間複雜度為
對途中每一個Edge做遍歷,透過找到可以通向那個Edge的Edge起點,再將那個點加入中
G Transpose 可以在,因此時間複雜度為
- Mark
There are two types of professional wrestlers: “babyfaces” (“good guys”) and
“heels” (“bad guys”). Between any pair of professional wrestlers, there may or may
not be a rivalry. Suppose we have n professional wrestlers and we have a list of r
pairs of wrestlers for which there are rivalries. Give an O(n + r)-time algorithm that
determines whether it is possible to designate some of the wrestlers as babyfaces
and the remainder as heels such that each rivalry is between a babyface and a heel.
If is it possible to perform such a designation, your algorithm should produce it.
思路
類比塗色問題,相鄰兩個點須為不同顏色 => 比賽的對戰組合類型永遠都是heel
v.s. babyface
,不同type
互打
步驟
令graph中的每個vertex有visited
、type
等屬性,對graph做BFS()
,以edge
作為對戰組合,若尚未經過(visited is false
)的就令他為與來源vertex
相反的type
,若已經過則觀察相鄰的vertex
是否為不同類型,可以的話就繼續搜尋直到結束,否則return false
。
若BFS()
是True
的情況,跑一個loop將所有vertex分配到對應babyface
、heel
的array,並回傳
蘇都扣
Time Complexity
- songchiu
Give a linear-time algorithm that takes as input a directed acyclic graph G = (V, E) and two vertices s and t, and returns the number of simple paths from s to t in G.
For example, the directed acyclic graph of Figure 22.8 contains exactly four simple paths from vertex p to vertex v: pov, poryv, posryv, and psryv. (Your algorithm needs only to count the simple paths, not list them.)
【解題思路】
用DFS來解,但稍微改良一下,讓它可以計算到底有幾條路徑符合
並且讓每個點多一個欄位,計算它的子節點總共有幾個路徑
【遞迴式】
【蘇都扣】
【時間複雜度】
每個點只會被走一遍,時間複雜度為
- yee
Give an algorithm that determines whether or not a given undirected graph G = (V,E) contains a cycle. Your algorithm should run in O(V) time, independent of |E|.
【解題思路】
當一個undirected graph跑DFS未產生back edges,表示其為acyclic。
因此只要做DFS時,產生一個back edge就代表存在cycle。
【蘇都扣】
【複雜度】
若有back edge,會在經過所有V前找到,若為acyclic,因<,也小於,因此時間複雜度為
- yee
Give an algorithm that determines whether or not a given undirected graph G = (V, E) contains an euler circuit,and if the answer is positive your algorithm should output an euler circuit.
【解題思路】
當一個graph每個V的degree皆為大於等於二的偶數,表示該graph有euler circuit。
李永樂老師解說
離散數學的ppt截下來的
【蘇都扣】
【複雜度】
判定是否為euler circuit需看過每個V的degree,需花,印出euler circuit需且判定移除edge是否為connected也需,因此時間複雜度為
印出euler circuit需多來存,因此空間複雜度為