Last Tutorial 11 Graph Graph – What is it? • G(V,E) – Collection of vertices V and edges E – Not a totally new.

Download Report

Transcript Last Tutorial 11 Graph Graph – What is it? • G(V,E) – Collection of vertices V and edges E – Not a totally new.

Last Tutorial 11
Graph
Graph – What is it?
• G(V,E)
– Collection of vertices V and edges E
– Not a totally new data structure, this is an extension from Linked List  Tree
• Very useful to model many (real-life) problems
– Internet
– Road Traffic
– Many others…
• Basic Terminologies (see q1!)
–
–
–
–
–
–
Vertices, Edges
Path, Cycle
Directed/Un-directed
Weighted/Un-weighted
Connected/Un-connected
In-degree/Out-degree
Graph – Basic Representations
• Adjacency Matrix
– 2-D array of vertices
– M[i][j] = 1 if there is an edge between vertex i and j, 0 otherwise
– For weighted graph, M[i][j] is the weight of the edges, not just 0 or 1!
• Adjacency List
– An array of V linked lists
– List i represent the vertices connected to vertex i
– For weighted graph, the list store both neighboring vertices and their edge costs
• Discussed in Q1
• There are more representations
– But we do not have time to discuss all…
Student Presentation
• Gr3 (average 2 times)
• Gr4 (average 3 times)
• Gr5 (average 4 times)
• Gr6 (average 3 times)
Overview of the questions:
1.
2.
3.
4.
Graph representations! (>1 students)
Graph traversals! (>1 students)
Topological Sort (1 student)
Mix and Match (floor)
4
Q1 – Graph Representations
• Explain:
– Characteristics of the Graph
• directed, weighted, connected*, has cycle
not dense, not too sparse either
– Adjacency List
HUB (3 in, 4 out)
Dead end (1 in, 0 out)
– Adjacency Matrix
Weight can also be associated with vertex!
• For small graph like this, both representations are ~ equally good.
– SGUK (10 days)SGTokyo is cheaper than SGTokyo (10 days)
Graph – Basic Algorithms (1)
• Graph traversal algorithms:
– Breadth First Search (BFS) O(V+E)
• O(V + E) is to say “take the max between V or E”.
For sparse graph, E < V.
For dense graph, E can be V2!
• Visit the graph breadth first
• Usually implemented using queue
– Depth First Search (DFS) O(V+E)
• Visit the graph depth first
• Usually implemented using recursion/stack
• Discussed in Q2
Q2 – Graph Traversals
• Start from Singapore:
• DFS
9K
– Answer: Singapore  Bali (cheapest),
backtrack  Tokyo  Shanghai,
backtrack, backtrack  London
• BFS
– Answer: It happens to be the same sequence:
Singapore  Bali (cheapest)  Tokyo  Shanghai  London
• If there is an edge (direct flight) of cost 9K from London to Shanghai
– And we start from London
• DFS
– London  Shanghai  Tokyo  Singapore  Bali
• BFS
– London 
Shanghai  Singapore 
Tokyo  Bali
(starting)
(1st layer)
(2nd layer)
Graph – Basic Algorithms (2)
• Topological Sort
– Topological Order:
• Linear order that does not violate precedence constraint,
e.g. underwear  outerwear *
– The algorithm: O(V+E)
• Insert vertex (or vertices) v with in-degree 0 in a queue
• While queue is not empty()
•
Take/process vertex v located in front of the queue
•
Decrease the in-degree of vertices adjacent to v by 1
– If in-degree becomes 0, add it into the queue
• Discussed in Q3
Q3 – Topological Sort
Note: This constraint
graph happens to be
a DAG. However,
not all constraint
graphs are DAGs!
• Valid topological order (starting from Australian GP):
– Australian GP, Malaysian GP, Monaco GP, French GP, British GP, Italian GP,
Belgian GP, Singapore GP, Japanese GP, Chinese GP, Abu Dhabi GP
• If Chinese GP  Japanese GP (a “funny” constraint)
– We cannot use topological sort! (Graph must be Directed Acyclic Graph/DAG)
Graph – Basic Algorithms (3)
• Shortest Path algorithm (not covered in CS1102 syllabus this sem)
– If the graph is un-weighted
• We can run BFS(), the level is the cost of the path O(V+E)
– If the graph is weighted
• Use Dijkstra Shortest Path Algorithm O((V+E) log V)
Graph – Basic Algorithms (4)
• There are many others…
– We cannot study all in this CS1102 module…
– Those who are interested to study more about Graph and other advanced
algorithms like Dynamic Programming, Greedy Algorithm, etc should take
CS3230 – Design and Analysis of Algorithms
Q4 – Mix and Match (1)
No Scenario
A database of SBS/SMRT Bus
1
Number and its itinerary list, e.g.
95: Buona Vista MRT NUS
Buona Vista MRT
96: Clementi MRT NUS
Clementi MRT
Which data structure/algorithm to use and why?
Direct Addressing Table (key: bus number) + Linked List
(itinerary)
O(1) to get the bus itinerary given bus number.
This scenario is from our lecture note and the answer is already
given.
We want to know the bus itinerary
given bus number.
2
3
Modeling friend connections in
friendster or facebook (social
networking site).
We want to be able to list down the
friends of a person and “friends of
friends” of a person (level-k friends).
We want to store a dictionary that
contains 1 million English words.
The natural representation for this problem is a graph.
Then, to answer the queries, we can simply do a kind of
Breadth first Search up to level-k. O(V+E).
Hash Table, static data like this is best stored in hash table.
Each lookup will be expected O(1).
Efficient lookup is important.
Q4 – Mix and Match (2)
4
Task scheduling in Operating System. Priority Queue (Heap), important tasks will be scheduled first.
Important process must be done first.
O(log n) for enqueue/dequeue!
5
In an election site, the organizer
wants to count the number of votes
for Barack Obama versus John
McCain. Suppose there are n voters
and their votes are written on n papers
(assume there is no invalid vote).
Store the data as an array of Bit String of size n
(0 for Obama, 1 for McCain),
Given a popular keyword, a search
engine may produce thousand hits.
We just want to display the top 10
most relevant hits!
Store the results as Heap O(n) –
actually can be pre-processed and stored in server,
and then for each query, take top O(k log n) results
where k=10 in this case.
6
then to count the number of votes,
one simple pass in O(n) will do.
Q4 – Mix and Match (3)
7
Storing text in Word editor like MS
Word. You want to efficiently support
navigational operations like: go to
next/previous character/word/
paragraph, etc.
Use hierarchical data structure, e.g. Tree.
The root is document. Document contains pages. Page contains
paragraphs. Paragraph contains words. Word contains
characters, etc…
Siblings in each level are connected with linked lists.
In this case, as far as navigation is concerned, we just need to
go to the relevant level and move to the next data. The only
benefit here is that if we create a new paragraph, all the
characters in the previous paragraph do not need to be updated
with the new value, the change is only one parameter at the
paragraph level.
Our Class Photo 
• You can see our photos here:
– http://www.comp.nus.edu.sg/~stevenha/myteaching/record.html
Advertisement
Food For Thought
• This is ‘the end’ of CS1102 tutorial
– Remember that you still have final exam!
• I hope you enjoy studying this module
– Regardless your final grade :$
• All the best for your final exam and future endeavor
• We may/may not meet again in the future
– But if we do, please greet each other 
– PS: Hopefully you are not in CS1102 class again next semester…
– Take CS3230 if you are interested to study advanced algorithms
Questions from Last Semester
• Use the remaining slides as extra exercises!
Q1 – Graph Representation
•
•
Show this graph’s 
Adjacency List:
•
Adjacency Matrix:
Q2 – Adjacency Matrix or List??
Would you use the adjacency list structure or the adjacency matrix structure in each of
the following cases? Justify your choice.
a) The graph has 10,000 vertices and 20,000 edges.
It is important to use as little space as possible.
Answer: Use adjacency list as there are on average 2 edges on each vertex.
We will waste a lot of memory space if adjacency matrix is used.
b) The graph has 10,000 vertices and 20,000,000 edges.
It is important to use as little space as possible.
Answer: Use adjacency matrix as there are on average 2000 edges on each vertex.
It would be much faster to find the neighbors of each vertex.
c) You need to answer the query areAdjacent as fast as possible.
No matter how much space you use.
Answer: Use adjacency Matrix since space is not a problem.
More importantly, the query areAdjacent can be answered in O(1) time.
Q3 – `Slow’ DFS??
Explain why the DFS traversal runs in O(n2) time
On an n-vertex simple graph that is represented with the adjacency matrix structure.
Answer:
This is because for each vertex,
We need to look at (n – 1) entries to see if they are the adjacent vertices.
Using Adjacency List, this DFS runs in O(V+E)
PS: A little note about V+E
The + sign means: whoever greater takes over this asymptotic value.
In sparse graph: E can be < V, O(V+E) = O(V)
In dense graph: E can be as many as V2, thus E >> V, O(V+E) = O(V+V2) = O(V2)
Q4 – DFS, BFS, Topological Sort
•
•
•
Do DFS and BFS starting from vertex `a’.
DFS/BFS traversal depends on
the ordering of neighbors…
Any valid DFS/BFS is accepted.
–
•
DFS:
–
–
–
•
a, b, c, d, e, f, h, g, i (my default answer) OR
a, d, c, b, h, f, e, g, i (if you adopt other vertex ordering) OR other possible sequences
Note that a, b, c, d, f, e, h, g, i is not an acceptable sequence, why?
We are using a queue, e will be inside first after visiting c, so e is executed first before f…
Homework:
–
•
a, b, c, d, f, e, g, i, h (my default answer) OR
a, b, c, d, h, f, g, e, i (if you adopt other vertex ordering) OR
a, b, c, e, i, g, d, h, f OR other possible sequences
BFS:
–
–
–
–
•
But usually we order neighbor alphabetically.
Try doing DFS/BFS from other nodes
Write the topological order of the vertices for the graph given above.
–
–
a, b, c, d, f, h, e, g, i (my default answer) OR
a, b, c, d, h, f, e, g, i (if you adopt other vertex ordering)
Q5 – Dijkstra
•
•
Do Shortest Path from `a’
using Dijkstra’s algorithm!
Homework:
– Try doing Shortest Path from ‘b’, ‘c’, ‘d’, etc…