COMP9024 ♢ Week 04a ♢ Graph Algorithms ♢ (22T0)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [0/67]
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [1/67]
❖ Directed Graphs (Digraphs) | |
In our previous discussion of graphs:
- an edge indicates a relationship between two vertices
- an edge indicates nothing more than a relationship
In many real-world applications of graphs:
- edges are directional (v → w ≠ w → v)
- edges have a weight (cost to go from v → w)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [2/67]
❖ ... Directed Graphs (Digraphs) | |
Example digraph and adjacency matrix representation:
Undirectional ⇒ symmetric matrix
Directional ⇒ non-symmetric matrix
Maximum #edges in a digraph with V vertices: V2
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [3/67]
❖ ... Directed Graphs (Digraphs) | |
Terminology for digraphs …
Directed path:
sequence of n ≥ 2 vertices v1 → v2 → … → vn
- where (vi,vi+1) ∈ edges(G) for all vi,vi+1 in sequence
- if v1 = vn, we have a directed cycle
Reachability:
w is reachable from
v if ∃ directed path
v,…,w
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [4/67]
Potential application areas:
Domain | | Vertex | | Edge |
Web | | web page | | hyperlink |
scheduling | | task | | precedence |
chess | | board position | | legal move |
science | | journal article | | citation |
dynamic data | | malloc'd object | | pointer |
programs | | function | | function call |
make | | file | | dependency |
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [5/67]
❖ ... Digraph Applications | |
Problems to solve on digraphs:
- is there a directed path from s to t?
(transitive closure)
- what is the shortest path from s to t?
(shortest path)
- are all vertices mutually reachable?
(strong connectivity)
- how to organise a set of tasks?
(topological sort)
- which web pages are "important"?
(PageRank)
- how to build a web crawler?
(graph traversal)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [6/67]
Similar set of choices as for undirectional graphs:
- array of edges (directed)
- vertex-indexed adjacency matrix (non-symmetric)
- vertex-indexed adjacency lists
V vertices identified by
0 .. V-1
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [7/67]
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [8/67]
Given a digraph G it is potentially useful to know
- is vertex t reachable from vertex s?
Example applications:
- can I complete a schedule from the current state?
- is a malloc'd object being referenced by any pointer?
How to compute transitive closure?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [9/67]
One possibility:
- implement it via
hasPath(G,s,t)
(itself implemented by DFS or BFS algorithm)
- feasible if reachable(G,s,t) is infrequent operation
What if we have an algorithm that frequently needs to check reachability?
Would be very convenient/efficient to have:
reachable(G,s,t):
| return G.tc[s][t]
Of course, if V is very large, then this is not feasible.
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [10/67]
❖ Exercise : Transitive Closure Matrix | |
Which reachable s .. t exist in the following graph?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [11/67]
Transitive closure of example graph:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [12/67]
Goal: produce a matrix of reachability values
- if tc[s][t] is 1, then t is reachable from s
- if tc[s][t] is 0, then t is not reachable from s
So, how to create this matrix?
Observation:
∀i,s,t ∈ vertices(G):
(s,i) ∈ edges(G) and (i,t) ∈ edges(G) ⇒ tc[s][t] = 1
tc[s][t]=1
if there is a path from s to t of length 2 (s→i→t)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [13/67]
If we implement the above as:
make tc[][] a copy of edges[][]
for all i∈vertices(G) do
for all s∈vertices(G) do
for all t∈vertices(G) do
if tc[s][i]=1 and tc[i][t]=1 then
tc[s][t]=1
end if
end for
end for
end for
then we get an algorithm to convert edges
into a tc
This is known as Warshall's algorithm
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [14/67]
How it works …
After iteration 1, tc[s][t]
is 1 if
- either s→t exists or s→0→t exists
After iteration 2,
tc[s][t]
is 1 if any of the following exist
- s→t or
s→0→t or
s→1→t or
s→0→1→t or
s→1→0→t
Etc. … so after the
Vth iteration,
tc[s][t]
is 1 if
- there is any directed path in the graph from s to t
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [15/67]
❖ Exercise : Transitive Closure | |
Trace Warshall's algorithm on the following graph:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [16/67]
1st iteration i=0
:
tc | [0] | [1] | [2] | [3] |
[0] | 0 | 1 | 1 | 1 |
[1] | 1 | 1 | 1 | 1 |
[2] | 0 | 1 | 0 | 0 |
[3] | 0 | 0 | 0 | 0 |
2nd iteration i=1
:
tc | [0] | [1] | [2] | [3] |
[0] | 1 | 1 | 1 | 1 |
[1] | 1 | 1 | 1 | 1 |
[2] | 1 | 1 | 1 | 1 |
[3] | 0 | 0 | 0 | 0 |
3rd iteration i=2
: unchanged
4th iteration i=3
: unchanged
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [17/67]
Cost analysis:
- storage: additional V2 items (each item may be 1 bit)
- computation of transitive closure: O(V3)
- computation of
reachable()
: O(1) after having generated tc[][]
Amortisation: would need many calls to
reachable()
to justify other costs
Alternative: use DFS in each call to reachable()
Cost analysis:
- storage: cost of queue and set during reachable
- computation of
reachable()
: cost of DFS = O(V2) (for adjacency matrix)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [18/67]
Same algorithms as for undirected graphs:
depthFirst(v):
- mark
v
as visited
- for each
(v,w)
∈edges(G) do
if w
has not been visited then
depthFirst(w)
breadth-first(v):
- enqueue
v
- while queue not empty do
dequeue v
if v
not already visited then
mark v
as visited
enqueue each vertex w
adjacent to v
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [19/67]
Goal: visit every page on the web
Solution: breadth-first search with "implicit" graph
webCrawl(startingURL):
| mark startingURL as alreadySeen
| enqueue(Q,startingURL)
| while Q is not empty do
| | nextPage=dequeue(Q)
| | visit nextPage
| | for each hyperLink on nextPage do
| | | if hyperLink not alreadySeen then
| | | mark hyperLink as alreadySeen
| | | enqueue(Q,hyperLink)
| | | end if
| | end for
| end while
visit
scans page and collects e.g. keywords and links
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [20/67]
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [21/67]
Graphs so far have considered
- edge = an association between two vertices/nodes
- may be a precedence in the association (directed)
Some applications require us to consider
- a cost or weight of an association
- modelled by assigning values to edges (e.g. positive reals)
Weights can be used in both directed and undirected graphs.
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [22/67]
Example: major airline flight routes in Australia
Representation: edge = direct flight; weight = approx flying time (hours)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [23/67]
Weights lead to minimisation-type questions, e.g.
1. Cheapest way to connect all vertices?
- a.k.a. minimum spanning tree problem
- assumes: edges are weighted and undirected
2. Cheapest way to get from
A to
B?
- a.k.a shortest path problem
- assumes: edge weights positive, directed or undirected
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [24/67]
❖ Exercise : Implementing a Route Finder | |
If we represent a street map as a graph
- what are the vertices?
- what are the edges?
- are edges directional?
- what are the weights?
- are the weights fixed?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [25/67]
❖ Weighted Graph Representation | |
Weights can easily be added to:
- adjacency matrix representation (0/1 → int or float)
- adjacency lists representation (add int/float to list node)
Both representations work whether edges are directed or not.
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [26/67]
❖ ... Weighted Graph Representation | |
Adjacency matrix representation with weights:
Note: need distinguished value to indicate "no edge".
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [27/67]
❖ ... Weighted Graph Representation | |
Adjacency lists representation with weights:
Note: if undirected, each edge appears twice with same weight
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [28/67]
❖ ... Weighted Graph Representation | |
Sample adjacency matrix implementation in C requires minimal changes to previous Graph ADT:
WGraph.h
typedef struct Edge {
Vertex v;
Vertex w;
int weight;
} Edge;
int adjacent(Graph, Vertex, Vertex);
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [29/67]
❖ ... Weighted Graph Representation | |
WGraph.c
typedef struct GraphRep {
int **edges;
int nV;
int nE;
} GraphRep;
void insertEdge(Graph g, Edge e) {
assert(g != NULL && validV(g,e.v) && validV(g,e.w));
if (g->edges[e.v][e.w] == 0) {
g->edges[e.v][e.w] = e.weight;
g->nE++;
}
}
int adjacent(Graph g, Vertex v, Vertex w) {
assert(g != NULL && validV(g,v) && validV(g,w));
return g->edges[v][w];
}
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [30/67]
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [31/67]
❖ Exercise : Minimising Wires in Circuits | |
Electronic circuit designs often need to make the pins of several components electrically equivalent by wiring them together.
To interconnect a set of n pins we can use an arrangement of n-1 wires each connecting two pins.
What kind of algorithm would …
- help us find the arrangement with the least amount of wire?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [32/67]
Reminder: Spanning tree ST of graph G=(V,E)
- spanning = all vertices, tree = no cycles
- ST is a subgraph of G
(G'=(V,E') where E' ⊆ E)
- ST is connected and acyclic
Minimum spanning tree MST of graph
G
- MST is a spanning tree of G
- sum of edge weights is no larger than any other ST
Applications: Computer networks, Electrical grids, Transportation networks …
Problem: how to (efficiently) find MST for graph G?
NB: MST may not be unique (e.g. all edges have same weight ⇒ every ST is MST)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [33/67]
❖ ... Minimum Spanning Trees | |
Example:
An MST …
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [34/67]
❖ ... Minimum Spanning Trees | |
Brute force solution:
findMST(G):
| Input graph G
| Output a minimum spanning tree of G
|
| bestCost=∞
| for all spanning trees t of G do
| | if cost(t)<bestCost then
| | bestTree=t
| | bestCost=cost(t)
| | end if
| end for
| return bestTree
Example of generate-and-test algorithm.
Not useful because
#spanning trees
is potentially large (e.g. nn-2 for a complete graph with n vertices)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [35/67]
❖ ... Minimum Spanning Trees | |
Simplifying assumption:
- edges in G are not directed
(MST for digraphs is harder)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [36/67]
One approach to computing MST for graph G with V nodes:
- start with empty MST
- consider edges in increasing weight order
- add edge if it does not form a cycle in MST
- repeat until V-1 edges are added
Critical operations:
- iterating over edges in weight order
- checking for cycles in a graph
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [37/67]
❖ ... Kruskal's Algorithm | |
Execution trace of Kruskal's algorithm:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [38/67]
❖ Exercise : Kruskal's Algorithm | |
Show how Kruskal's algorithm produces an MST on:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [39/67]
After 3rd iteration:
After 6th iteration:
After 7th iteration:
After 8th iteration (V-1=8 edges added):
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [40/67]
❖ ... Kruskal's Algorithm | |
Pseudocode:
KruskalMST(G):
| Input graph G with n nodes
| Output a minimum spanning tree of G
|
| MST=empty graph
| sort edges(G) by weight
| for each e∈sortedEdgeList do
| | MST = MST ∪ {e}
| | if MST has a cyle then
| | MST = MST \ {e}
| | end if
| | if MST has n-1 edges then
| | return MST
| | end if
| end for
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [41/67]
❖ ... Kruskal's Algorithm | |
Rough time complexity analysis …
- sorting edge list is O(E·log E)
- at least V iterations over sorted edges
- on each iteration …
- getting next lowest cost edge is O(1)
- checking whether adding it forms a cycle: cost = ??
Possibilities for cycle checking:
- use DFS … too expensive?
- could use Union-Find data structure (see Sedgewick Ch.1)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [42/67]
Another approach to computing MST for graph G=(V,E):
- start from any vertex v and empty MST
- choose edge not already in MST to add to MST
- must be incident on a vertex s already connected to v in MST
- must be incident on a vertex t not already connected to v in MST
- must have minimal weight of all such edges
- repeat until MST covers all vertices
Critical operations:
- checking for vertex being connected in a graph
- finding min weight edge in a set of edges
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [43/67]
Execution trace of Prim's algorithm (starting at s=0):
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [44/67]
❖ Exercise : Prim's Algorithm | |
Show how Prim's algorithm produces an MST on:
Start from vertex 0
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [45/67]
After 1st iteration:
After 2nd iteration:
After 3rd iteration:
After 4th iteration:
After 8th iteration (all vertices covered):
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [46/67]
Pseudocode:
PrimMST(G):
| Input graph G with n nodes
| Output a minimum spanning tree of G
|
| MST=empty graph
| usedV={0}
| unusedE=edges(g)
| while |usedV|<n do
| | find e=(s,t,w)∈unusedE such that {
| | s∈usedV, t∉usedV and w is min weight of all such edges
| | }
| | MST = MST ∪ {e}
| | usedV = usedV ∪ {t}
| | unusedE = unusedE \ {e}
| end while
| return MST
Critical operation: finding best edge
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [47/67]
Rough time complexity analysis …
- V iterations of outer loop
- in each iteration …
- find min edge with set of edges is O(E) ⇒ O(V·E) overall
- find min edge with priority queue is O(log E) ⇒ O(V·log E) overall
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [48/67]
❖ Sidetrack: Priority Queues | |
Some applications of queues require
- items processed in order of "priority"
- rather than in order of entry (FIFO — first in, first out)
Priority Queues (
PQueues) provide this via:
-
join
: insert item into PQueue with an associated priority (replacing enqueue
)
-
leave
: remove item with highest priority (replacing dequeue
)
Time complexity for naive implementation of a PQueue containing
N items …
- O(1) for
join
O(N) for leave
Most efficient implementation ("heap") …
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [49/67]
Boruvka's algorithm … complexity O(E·log V)
- the oldest MST algorithm
- start with V separate components
- join components using min cost links
- continue until only a single component
Karger, Klein, and Tarjan … complexity
O(E)
- based on Boruvka, but non-deterministic
- randomly selects subset of edges to consider
- for the keen, here's the paper describing the algorithm
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [50/67]
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [51/67]
Path = sequence of edges in graph G p = (v0,v1), (v1,v2), …, (vm-1,vm)
cost(path) = sum of edge weights along path
Shortest path between vertices s and t
- a simple path p(s,t) where s = first(p), t = last(p)
- no other simple path q(s,t) has cost(q) < cost(p)
Assumptions: weighted digraph, no negative weights.
Finding shortest path between two given nodes known as source-target SP problem
Variations: single-source SP, all-pairs SP
Applications: navigation, routing in data networks, …
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [52/67]
❖ Single-source Shortest Path (SSSP) | |
Given: weighted digraph G, source vertex s
Result: shortest paths from s to all other vertices
-
dist[]
V-indexed array of cost of shortest path from s
-
pred[]
V-indexed array of predecessor in shortest path from s
Example:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [53/67]
Assume: dist[]
and pred[]
as above (but containing data for shortest paths discovered so far)
dist[v]
is length of shortest known path from s to v
dist[w]
is length of shortest known path from s to w
Relaxation updates data for w if we find a shorter path from s to w:
Relaxation along edge e=(v,w,weight)
:
- if
dist[v]+weight < dist[w]
then
update dist[w]:=dist[v]+weight
and pred[w]:=v
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [54/67]
One approach to solving single-source shortest path problem …
Data: G, s, dist[]
, pred[]
and
- vSet: set of vertices whose shortest path from s is unknown
Algorithm:
dist[]
pred[]
dijkstraSSSP(G,source):
| Input graph G, source node
|
| initialise dist[] to all ∞, except dist[source]=0
| initialise pred[] to all -1
| vSet=all vertices of G
| while vSet≠∅ do
| | find s∈vSet with minimum dist[s]
| | for each (s,t,w)∈edges(G) do
| | relax along (s,t,w)
| | end for
| | vSet=vSet\{s}
| end while
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [55/67]
❖ Exercise : Dijkstra's Algorithm | |
Show how Dijkstra's algorithm runs on (source node = 0):
|
[0] | [1] | [2] | [3] | [4] | [5] |
dist |
0 | ∞ | ∞ | ∞ |
∞ | ∞ |
pred |
– | – | – |
– | – | – |
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [56/67]
|
[0] | [1] | [2] | [3] | [4] | [5] |
dist |
0 | ∞ | ∞ | ∞ |
∞ | ∞ |
pred |
– | – | – |
– | – | – |
dist |
0 | 14 | 9 | 7 |
∞ | ∞ |
pred |
– | 0 | 0 |
0 | – | – |
dist |
0 | 14 | 9 | 7 |
∞ | 22 |
pred |
– | 0 | 0 |
0 | – | 3 |
dist |
0 | 13 | 9 | 7 |
∞ | 12 |
pred |
– | 2 | 0 |
0 | – | 2 |
dist |
0 | 13 | 9 | 7 |
20 | 12 |
pred |
– | 2 | 0 |
0 | 5 | 2 |
dist |
0 | 13 | 9 | 7 |
18 | 12 |
pred |
– | 2 | 0 |
0 | 1 | 2 |
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [57/67]
❖ ... Dijkstra's Algorithm | |
Why Dijkstra's algorithm is correct:
Hypothesis.
(a) For visited s … dist[s] is shortest distance from source
(b) For unvisited t … dist[t] is shortest distance from source via visited nodes
Proof.
Base case: no visited nodes, dist[source]=0, dist[s]=∞ for all other nodes
Induction step:
- If s is unvisited node with minimum dist[s], then dist[s] is shortest distance from source to s:
- if ∃ shorter path via only visited nodes, then dist[s] would have been updated when processing the predecessor of s on this path
- if ∃ shorter path via an unvisited node u, then dist[u]<dist[s], which is impossible if s has min distance of all unvisited nodes
- This implies that (a) holds for s after processing s
- (b) still holds for all unvisited nodes t after processing s:
- if ∃ shorter path via s we would have just updated dist[t]
- if ∃ shorter path without s we would have found it previously
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [58/67]
❖ ... Dijkstra's Algorithm | |
Time complexity analysis …
Each edge needs to be considered once ⇒ O(E).
Outer loop has O(V) iterations.
Implementing "find s∈vSet with minimum dist[s]
"
- try all
s
∈vSet
⇒ cost = O(V) ⇒ overall cost = O(E + V2) = O(V2)
- using a PQueue to implement extracting minimum
- can improve overall cost to O(E + V·log V)
(for best-known implementation)
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [59/67]
❖ All-pair Shortest Path (APSP) | |
Given: weighted digraph G
Result: shortest paths between all pairs of vertices
-
dist[][]
V×V-indexed matrix of cost of shortest path from vrow to vcol
-
path[][]
V×V-indexed matrix of next node in shortest path from vrow to vcol
Example:
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [60/67]
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [61/67]
Goal: determine which Web pages are "important"
Approach: ignore page contents; focus on hyperlinks
- treat Web as graph: page = vertex, hyperlink = directed edge
- pages with many incoming hyperlinks are important
- need to compute "incoming degree" for vertices
Problem: the Web is a
very large graph
- approx. 1014 pages, 1015 hyperlinks
Assume for the moment that we could build a graph …
Most frequent operation in algorithm "Does edge (v,w) exist?"
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [62/67]
Simple PageRank algorithm:
PageRank(myPage):
| rank=0
| for each page in the Web do
| | if linkExists(page,myPage) then
| | rank=rank+1
| | end if
| end for
Note: requires inbound link check
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [63/67]
V = # pages in Web, E = # hyperlinks in Web
Costs for computing PageRank for each representation:
Representation | |
linkExists(v,w) | |
Cost |
Adjacency matrix | |
edge[v][w] | |
1 |
Adjacency lists | |
inLL(list[v],w) | |
≅ E/V |
Not feasible …
- adjacency matrix … V ≅ 1014 ⇒ matrix has 1028 cells
- adjacency list … V lists, each with ≅10 hyperlinks ⇒ 1015 list nodes
So how to really do it?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [64/67]
Approach: the random web surfer
- if we randomly follow links in the web …
- … more likely to re-discover pages with many inbound links
curr=random page, prev=null
for a long time do
| if curr not in array ranked[] then
| rank[curr]=0
| end if
| rank[curr]=rank[curr]+1
| if random(0,100)<85 then
| prev=curr
| curr=choose hyperlink from curr
| else
| curr=random page
| prev=null
| end if
end for
Could be accomplished while we crawl web to build search index
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [65/67]
❖ Exercise : Implementing Facebook | |
Facebook could be considered as a giant "social graph"
- what are the vertices?
- what are the edges?
- are edges directional?
What kind of algorithm would …
- help us find people that you might like to "befriend"?
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [66/67]
- Digraphs, weighted graphs: representations, applications
- Reachability
- Minimum Spanning Tree (MST)
- Shortest path problems
- Dijkstra (single source SPP)
- Floyd (all-pair SSP)
- Flow networks
- Edmonds-Karp (maximum flow)
- Suggested reading (Sedgewick):
- digraphs … Ch. 19.1-19.3
- weighted graphs … Ch. 20-20.1
- MST … Ch. 20.2-20.4
- SSP … Ch. 21-21.3
- network flows … Ch. 22.1-22.2
COMP9024 (22T0) ♢ Week 04a ♢ Graph Algorithms ♢ [67/67]
Produced: 20 Dec 2021