CS1102 Tut 10 Graphs - PowerPoint PPT Presentation

1 / 49
About This Presentation
Title:

CS1102 Tut 10 Graphs

Description:

But we may do BFS on all remaining nodes until every node is traveled. ... subsequent trees since the master page would have traveled to all other nodes! ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 50
Provided by: soc128
Category:
Tags: cs1102 | graphs | traveled | tut

less

Transcript and Presenter's Notes

Title: CS1102 Tut 10 Graphs


1
CS1102 Tut 10 Graphs
  • Max Tan
  • tanhuiyi_at_comp.nus.edu.sg
  • S15-03-07 Tel65164364http//www.comp.nus.edu.sg
    /tanhuiyi

2
First 15 minutes
  • PE this Saturday 1230, PL 1 to 4 (same venue as
    mock PE. 2hrs 30 minutes.
  • Please try to reach before 12
  • Come well rested, an alert mind works faster
  • Predicted a lot of people with stomachaches,
    fevers, so watch your intake the previous day
  • 2 Questions 1st question 30 2nd question 70 (if
    I am not wrong)
  • Open book, you may bring anything you like (but I
    can tell you it doesnt matter much)

3
First 15 minutes
  • First half hour for you to READ and UNDERSTAND
  • Please read instructions carefully, you will have
    to do your coding in the correct folders
  • TA or Tutors can answer general queries to help
    in understanding questions, cannot help you look
    at your code
  • Please try to familiarize yourself with the unix,
    learn how to create a new folder etc.
  • Your pain and suffering is over after this (at
    least for labs)

4
Question 1
  • Graph can be used to represent the collection
  • Pages are nodes, Links are edges
  • What is the easy way to find all pages in C which
    are master pages?
  • Just do BFS on each node and output nodes which
    can traverse all other nodes!

5
Question 1
  • Time complexity O(V (V E))
  • Can we do better ?

6
Question 1
  • Assume that there EXISTS a master page.
  • Observation 1 A page which can lead to a master
    page is a master page
  • This is because by definition of a master page
    which can lead to all possible pages, a page that
    can lead to a master page will then lead to all
    possible pages, hence it is a master page

7
Question 1
  • Observation 2 BFS on any random node may not
    reach all other nodes. But we may do BFS on all
    remaining nodes until every node is traveled. We
    get a forest of BFS trees

8
Question 1
  • Claim If there is a master page, then the root
    of the last tree is a master page

Must be master page!
9
Question 1
  • Prove by contradiction assume that the root of
    the last BFS tree is not a master page

Must be master page!
10
Question 1
  • If the master page is say, in other trees, we
    wont have any subsequent trees since the master
    page would have traveled to all other nodes!

This is master page
11
Question 1
  • Hence if the root of the last tree is not a
    master page, then there is no master page, which
    contradicts our assumption on the existence of a
    master page!

This is master page
12
Question 1
  • After you have found one master page, it is a
    simple matter of determining the parent nodes of
    the master page.
  • Simply reverse all the edges, and do DFS from the
    master page and you will get all other master
    pages
  • Time complexity
  • O(V E)

13
Question 1
  • To find the master page that minimizes the
    maximal number of links
  • So we need to find the center of this graph, if
    we can find the center, then it must be the
    master page that minimizes the maximal number of
    links

14
Question 1
  • Define height as the minimal distance from the
    root to its leaves
  • Simple way is to do the BFS on the master pages
    to find the height of that master page
  • Just recursively find the height of a nodes
    children and take the maximum

15
Question 2
  • Generic BFS DFS traversal
  • More then 1 unique answer

16
Question 2 - BFS
  • A B C D H F G E I
  • A B C E I G D H F

17
Question 2 - DFS
  • A B C D E F H I G
  • A D C B H F E G I

18
Question 2 - DFS
  • In degree
  • Total in-degree is just the sum of the edges

19
Question 2 - DFS
  • A 0
  • B 2
  • C 4
  • D 6

20
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 6

21
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5

22
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8

23
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 10
  • I 8

24
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

25
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

26
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

27
Question 2 - DFS
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

28
Question 3
  • Important invariant that MUST hold true in each
    step Dijkstras algorithm
  • Whenever we choose the closest node in each step,
    there is no possible way that there is a node
    closer then that
  • Invariant holds true when we sum up the cost of
    the path
  • Will it hold true when we multiply the cost of
    the path with decimal values?

29
Question 3
  • Consider this casewhere y is the closest node
  • Is it possible that there exist another path that
    would make y cheaper ?

y
P(x)
x
z
30
Question 3
  • Consider this casewhere y is the closest node
  • Take P(y) P(x) P(xy)
  • If there exist a shorter path, that means there
    is a series of path to Z then to y

y
P(x)
P(xy)
x
P(zy)
z
31
Question 3
  • Counter example!
  • Take P(y) P(x) 0.3
  • Shorter path
  • P(y) 0.4 0.2
  • P(y) 0.08
  • Dijkstras algorithm will NOT work!

y
P(x)
0.3
x
0.2
z
0.4
32
Question 3
0.3, A
0.7, Q
0.7, V
0.2, L
0.9, E
0.4, Q
0.5, V
0.2, Y
0.2, I
0.1, R
33
Question 3
  • The idea is to use topological sort and compute
    the score of each node
  • To obtain the score of a node, we take the parent
    with the least score to compute its new score
  • However one key assumption we have to make is
    that there is no cycles!

34
Question 3
0.3, A
0.7, Q
0.7, V
0.2, L
0.9, E
1
0.4, Q
0.5, V
0.2, Y
0.2, I
0.1, R
35
Question 3
0.7
0.2
1
0.4
36
Question 3
0.18
0.7
0.18
0.2
1
0.4
0.2
0.08
37
Question 3
0.18 0.7
0.7
0.18
0.126
0.2
1
0.4 0.5 0.2
0.4
0.2
0.04
0.4 0.2 0.1
0.08
0.08
38
Question 4
  • This question is asking about a new concept in
    graphs
  • The full name for s-tree is called spanning trees
    (I didnt put the full name because I didnt want
    you to google the answer)
  • Minimum spanning tree is a tree containing all
    vertices where the sum of edges is minimum

39
Question 4
  • We can use a greedy algorithm (like Dijkstras
    algorithm)
  • The start point doesnt matter now, all we want
    is to be able to get the sum of edges such that
    the sum is minimal
  • So we just keep choosing the minimal edges each
    time until the graph is connected
  • Does it work?

40
Question 4
  • Suppose we choose the edges ab and ac for the
    spanning tree using our algorithm
  • Is it possible that there is a smaller spanning
    tree using edges ac, cb ?
  • No! because if we choose cb instead of ab, it
    would contradict the fact that cb gt ab!
  • Hence our algorithm will simply choose the
    smallest edge from the unconnected components
    each time to grow the tree

b
3
a
7
4
c
41
Question 4
  • We can start from any node, since we must
    connect all nodes
  • So we just start from node S (does it matter
    where you start?)

42
Question 4
  • Pick edge Sa

43
Question 4
  • Pick edge Sb

44
Question 4
  • Pick edge be

45
Question 4
  • Pick edge eg

46
Question 4
  • Pick edge gd

47
Question 4
  • Pick edge ac

48
Question 4
  • Pick edge ad then df

49
Question 4
5
d
3
a
1
3
f
S
c
D
2
2
b
g
e
2
2
  • Spanning tree!
  • For code, see the answer
Write a Comment
User Comments (0)
About PowerShow.com