CS1102 Tut 10 Graphs - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

CS1102 Tut 10 Graphs

Description:

Question 1 Group 1. Question 2 Group 2. Question 3 Group 3 ... Idea is, Nodes with incoming edges has pre-reqs. Choose nodes with NO prereqs first ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 38
Provided by: dcsa
Category:
Tags: cs1102 | graphs | incoming | 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
Groups Assignment
  • Question 1 Group 1
  • Question 2 Group 2
  • Question 3 Group 3
  • Question 4 Group 4
  • Question 5 Group 1
  • Question 6 Group 2

3
Groups Assignment
  • Question 1 Group 2
  • Question 2 Group 3
  • Question 3 Group 1
  • Question 4 Group 2
  • Question 5 Group 3
  • Question 6 Group 1

4
First 15 minutes
  • Breadth first search
  • Depth first search
  • Topological Sort
  • Shortest path

5
First 15 minutes BFS
  • Breadth first search is like ripples in a pond,
    the ripples spread outwards
  • Breadth first search is like level order
    traversal in a tree
  • Difference is in a tree, once a root is traversed
    it will never be reached again

Hence, you need to remember which node has been
traveled before otherwise you will go into an
infinite loop!
6
First 15 minutes
  • Algorithm
  • 1) Dequeue. If I encounter a node Ive never seen
    before, I print it and enqueue its adj nodes.
  • 2) Remember that I have seen the node before.
  • 3) Repeat 1 until queue is empty

7
First 15 minutes DFS
  • Depth first search is like solving a maze
  • Travel as far as you can before you reach a dead
    end, then go back to the last junction and try a
    different route (which you have not explored).
  • Routes may intersect!

There may be cycles, so your depth first search
must be careful
8
First 15 minutes
  • Algorithm
  • 1) Peek node from top of stack (this is the last
    junction traveled)
  • 2) If there is an unexplored route, try that
    route, otherwise pop from stack (try previous
    junction)

9
First 15 minutes
  • Algorithm
  • 1) Peek node from top of stack (this is the last
    junction traveled)
  • 2) If there is an unexplored route, try that
    route, otherwise pop from stack (try previous
    junction)

10
First 15 minutes TSORT
  • Topological sort is to obtain the solution to
    take modules which fulfills pre-req requirements
  • Idea is, Nodes with incoming edges has pre-reqs
  • Choose nodes with NO prereqs first
  • After node is taken, remove the node AND all its
    corresponding outgoing edges

11
First 15 minutes
  • Topological sort is to obtain the solution to
    take modules which fulfills pre-req requirements
  • Idea is, Nodes with incoming edges has pre-reqs
  • Choose nodes with NO prereqs first
  • After node is taken, remove the node AND all its
    corresponding outgoing edges

cs2103
cs1101
cs2105
cs1104
CS1101
CS1102
12
First 15 minutes Dijkstras
  • Dijkstras algorithm is a shortest path algorithm
  • It is a greedy algorithm (pick the smallest each
    time) and it is optimum (gives the best answer)

13
Question 1
  • Give the adjacency matrix and the adjacency list
    for the following directed graph.

14
Question 1
15
Question 1
16
Question 2
  • Assume that there is a method that will return
    the index of a vertex in the adjacency matrix and
    the adjacency list in O(1) time.
  • Discuss the pros and cons of using adjacency
    matrix and adjacency list to represent graphs and
    the running times of the operations
  • To insert a vertex (together with all its edges)
  • To delete a vertex (together with all its edges),
    and
  • To check whether a vertex is adjacent to another
    vertex.

17
Question 2
  • List vs Matrix
  • Matrix needs O(V2) space
  • List needs O(V E) space
  • Complete graph O(V2) for both
  • Sparse graph (only 1 or 2 edges per vertex),
    wastage of space in a matrix representation!

18
Question 2
  • Run time
  • InsertionMatrix O(k), where k is total number
    of vertices affectedList O(k), where k is
    total number of vertices affected

19
Question 2
  • Run time
  • DeletionMatrix O(v) (just set rowi and columni
    to 0)List O(E), Have to scan all the lists to
    find v and remove from list. Total is O(E) time

20
Question 2
  • Run time
  • Check adjacencyMatrix O(1) List O(k), Scan
    through the list of B to determine if A is in the
    list

21
Question 3
  • Use both the depth-first strategy and the
    breadth-first strategy to traverse the following
    graph beginning with vertex a. List the vertices
    in the order in which each traversal visits them.

22
Question 3 - DFS
  • A B C D H F G E I
  • A B C E I G D H F

23
Question 3 - BFS
  • A B C D E F H I G
  • A D C B H F E G I

24
Question 4
  • List down the in-degrees of all the vertices in
    the graph given in Question 3. What do you obtain
    when you add up the in-degrees of all the
    vertices?

25
Question 4
  • Total is equals to the number of edges!

26
Question 5
  • Write two topological orders of the vertices for
    the graph.
  • a, b, c, d, h, f, e, g, i
  • a, b, c, d, f, h, e, g, i

27
Question 6 Shortest path
  • A 0
  • B 8 ? 2
  • C 8 ? 4
  • D 8 ? 6

28
Question 6 Shortest path
  • A 0
  • B 2
  • C 4 ? 3
  • D 6

29
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 6 ? 4
  • E 8 ? 5

30
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 8 ? 7
  • H 8 ? 8

31
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 8 ? 10
  • I 8 ? 8

32
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 10 ? 9
  • I 8

33
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

34
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

35
Question 6 Shortest path
  • A 0
  • B 2
  • C 3
  • D 4
  • E 5
  • F 7
  • H 8
  • G 9
  • I 8

36
Question 6
37
End
  • Good luck for your exams!
Write a Comment
User Comments (0)
About PowerShow.com