22. Representations of graphs

There are 2 ways to display a graph:

  1. adjacency list
  2. adjacency matrix

The space and time complexity of the matrix are n^2, which is quite large and inefficient for the practice.

Graph searching techniques:

  1. Breadth First Search (BFS)
  2. Depth First Search (DFS)

1. Breadth First Search

To keep track of BFS search progress, we color each vertex in a graph with one of following three colors.
a. WHITE – not visited yet
b. GRAY – visited but don’t check all its neighbors yet
c. BLACK – visited and finished looking at all its neighbors

To implement this search, we need a data structure:

  1. Queue: dequeue and enqueue.
  2. 3 attributes of the vertices:
    color : WHITE, GRAY or BLACK
    distance : an integer (the distance of v from the starting vertex)
    pointer: the parent pointer that points to another vertex

Property of BFS

  1. All the vertices reachable from s are visited in order of their distance from s.
  2. The final value of v. d is infinite if v is not reachable from s, and the distance
  3. The edges (v. pointer, v) for all v belongs to V together form a tree.
    from s to v otherwise.

Time complexity: Each vertex is initialized once and put into the queue once.
Each edge is examined twice from each of its two endpoints.
Therefore, the total time for BFS traversal on a graph G(V,E) is O(|V|+|E|), where |V| is the number of vertices and |E| is the number of edges.

Application

  1. Distances in a graph.(Note that this is only for graphs where all edges have the same length.)
  2. Connected components of an undirected graph.
  3. Coloring a bipartite graph.
    An undirected graph G(V,E) is a bipartite graph if its vertex set can be colored with two colors such that every edge has one endpoint of each color.

Depth First Search -- backtracking search

Moves “forward” when possible, and only “backtracks” when moving forward is impossible.
Data structure:
The Stack should be used instead of Queue. (This stack can either by represented explicitly (by a stack data-type in our language) or implicitly when using recursive functions.)

As well as the BFS, there are more attributes:

  1. time : a global clock with values 0,1,2,...
  2. v.d : discovery time (when v is first visited)
  3. v. f : finishing time (when all the neighbours of v have been examined)
59E9C0B6-896F-4E6C-BC10-5AB71B65A54A.png

.

Running Time
First of all, the vertices need to be initialized( |v| ), then DFS will work on all the neighbours (|E|).

Edge classification in DFS

DFS classifies the edges in a directed graph into four types.

  1. Tree edges (edges along which a vertex is first discovered)
  2. Back edges (edges from a descendant to an ancestor)
  3. Forward edges (non-tree edges from an ancestor to a descendant)
  4. Cross edges (all other edges)

In an undirected graph, edges (u, v) and (v, u) are the same, so we cannot distinguish between forward edges and back edges.
For such a graph, a non-tree edge between a pair of vertices that one is an ancestor of the other is referred to as a back edge.
Theorem: In a depth-first search of an undirected graph G, each edge of G is either a tree edge or a back edge.

Topological Sorting

A directed graph G(V,E) is called acyclic if it does not contain a directed cycle. Such a graph is also referred to as a Directed Acyclic Graph (DAG).

Theorem
A directed graph G has a directed cycle if and only if a depth-first search of G yields a back edge.
Theorem
Perform DFS on a directed acyclic graph G. Then, the vertex sequence listed by the decreasing order of their finishing time forms a topological ordering for G.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末怀大,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子呀闻,更是在濱河造成了極大的恐慌化借,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件捡多,死亡現(xiàn)場(chǎng)離奇詭異蓖康,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)垒手,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門蒜焊,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人淫奔,你說我怎么就攤上這事山涡。” “怎么了唆迁?”我有些...
    開封第一講書人閱讀 164,411評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵鸭丛,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我唐责,道長(zhǎng)鳞溉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評(píng)論 1 293
  • 正文 為了忘掉前任鼠哥,我火速辦了婚禮熟菲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘朴恳。我一直安慰自己抄罕,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評(píng)論 6 392
  • 文/花漫 我一把揭開白布于颖。 她就那樣靜靜地躺著呆贿,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上做入,一...
    開封第一講書人閱讀 51,521評(píng)論 1 304
  • 那天冒晰,我揣著相機(jī)與錄音,去河邊找鬼竟块。 笑死壶运,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的浪秘。 我是一名探鬼主播蒋情,決...
    沈念sama閱讀 40,288評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼秫逝!你這毒婦竟也來(lái)了恕出?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤违帆,失蹤者是張志新(化名)和其女友劉穎浙巫,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體刷后,經(jīng)...
    沈念sama閱讀 45,644評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡的畴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了尝胆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丧裁。...
    茶點(diǎn)故事閱讀 39,953評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖含衔,靈堂內(nèi)的尸體忽然破棺而出煎娇,到底是詐尸還是另有隱情,我是刑警寧澤贪染,帶...
    沈念sama閱讀 35,673評(píng)論 5 346
  • 正文 年R本政府宣布缓呛,位于F島的核電站,受9級(jí)特大地震影響杭隙,放射性物質(zhì)發(fā)生泄漏哟绊。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評(píng)論 3 329
  • 文/蒙蒙 一痰憎、第九天 我趴在偏房一處隱蔽的房頂上張望票髓。 院中可真熱鬧,春花似錦铣耘、人聲如沸洽沟。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)玲躯。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間跷车,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工橱野, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留朽缴,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,119評(píng)論 3 370
  • 正文 我出身青樓水援,卻偏偏與公主長(zhǎng)得像密强,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蜗元,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容