Upload
dinhtu
View
212
Download
0
Embed Size (px)
Citation preview
Introduction to graphs
• Undirected graphs• Representation • Depth first search
• Connected components• Breadth first search
• Bipartite graphs
Introduction to graphs
• Undirected graphs• Representing graphs • Depth first search
• Connected components• Breadth first search
• Bipartite graphs
• Undirected graph. Set of vertices (da: knuder) pairwise joined by edges (da: kanter).
• Why graphs?• Models many problems from different areas.• Thousands of practical applications.• Hundreds of well-known graph algorithms.
Undirected graphs
0
7
9 10
1211
1
3
2
5
6 8
4
vertex Edge
Visualisation of the internet
http://en.wikipedia.org/wiki/Opte_Project
Visualisation of friendships on Facebook
"Visualizing friendships", Paul Butler
London Metro
River Thames
A
B
C
D
E
F
1 2 3 4 5 6 7 8 9
1 2 3 4 5 76 8 9
A
B
C
D
E
F
2 2
22
2
5
8 8 6
2
4
4
65
41
3
2
43
3
36 3 1
1
3
3
59 7 7Special fares apply
5
5
4
4
4
AmershamChorleywood
Mill Hill East
Rickmansworth
Perivale
KentishTown West
CamdenRoad
Dalston Kingsland
Wanstead Park
Vauxhall
Hanger Lane
Edgware
Burnt Oak
Colindale
Hendon Central
Brent Cross
Golders Green
WestSilvertown
EmiratesRoyal Docks
EmiratesGreenwichPeninsula Pontoon Dock
LondonCity Airport
WoolwichArsenal
King George V
Hampstead
Belsize Park
Chalk Farm
Chalfont &Latimer
Chesham
New CrossGate
Moor Park
NorthwoodNorthwoodHills
Pinner
North Harrow
Custom House for ExCeL
Prince Regent
Royal Albert
Beckton Park
Cyprus
GallionsReach
Beckton
Watford
Croxley
Fulham Broadway
LambethNorth
HeathrowTerminal 4
Harrow-on-the-Hill
KensalRise
BethnalGreen
Westferry
SevenSisters
Blackwall
BrondesburyPark
HampsteadHeath
HarringayGreen Lanes
LeytonstoneHigh Road
LeytonMidland Road
HackneyCentral
NorthwickPark
PrestonRoad
RoyalVictoria
WembleyPark
Rayners Lane
Watford High Street
RuislipGardens
South Ruislip
Greenford
Northolt
South Harrow
Sudbury Hill
Sudbury Town
Alperton
Pimlico
Park Royal
North Ealing
Acton Central
South Acton
Ealing Broadway
Watford Junction
West Ruislip
Bushey
Carpenders Park
Hatch End
North Wembley
West Brompton
Ealing Common
South Kenton
Kenton
Wembley Central
Kensal Green
Queen’s Park
Gunnersbury
Kew Gardens
Richmond
Stockwell
Bow Church
Stonebridge Park
Harlesden
Camden Town
Willesden Junction
Headstone Lane
Parsons Green
Putney Bridge
East Putney
Southfields
Wimbledon Park
Wimbledon
Island Gardens
Greenwich
Deptford Bridge
South Quay
Crossharbour
Mudchute
Heron Quays
West India Quay
Elverson Road
Oakwood
Cockfosters
Southgate
Arnos Grove
Bounds Green
Theydon Bois
Epping
Debden
Loughton
Buckhurst Hill
WalthamstowQueen’s Road
Woodgrange Park
Leytonstone
Leyton
Wood Green
Turnpike Lane
Manor House
Stanmore
Canons Park
Queensbury
Kingsbury
High Barnet
Totteridge & Whetstone
Woodside Park
West Finchley
Finchley CentralWoodford
South Woodford
Snaresbrook
Hainault
Fairlop
Barkingside
Newbury Park
East Finchley
Highgate
Archway
Devons Road
Langdon Park
All Saints
Tufnell Park
Kentish Town
Neasden
Dollis Hill
Willesden Green
South Tottenham
Swiss Cottage
ImperialWharf
Brixton
Kilburn
West Hampstead
Blackhorse Road
Acton Town
CanningTown
Finchley Road
Highbury &Islington
Canary Wharf
Stratford
StratfordInternational
FinsburyPark
Elephant & Castle
Stepney Green
Barking
East Ham
Plaistow
Upton Park
Poplar
West Ham
Upper Holloway
PuddingMill Lane
Kennington
Borough
Elm ParkDagenham
East
DagenhamHeathway
Becontree
Upney
Heathrow Terminal 5
Finchley Road& Frognal
CrouchHill
Northfields
Boston Manor
South Ealing
Osterley
Hounslow Central
Hounslow East
Clapham North
Clapham High Street
Oval
Arsenal
Holloway Road
Caledonian Road
West Croydon
HounslowWest
Hatton Cross
HeathrowTerminals 1, 2, 3
ClaphamJunction
WestHarrow
Brondesbury CaledonianRoad &
Barnsbury
TottenhamHale
WalthamstowCentral
HackneyWick
Homerton
WestActon
Limehouse EastIndia
Crystal Palace
ChiswickPark
RodingValley
GrangeHill
Chigwell
Redbridge
GantsHill
Wanstead
Ickenham
TurnhamGreen
Uxbridge
Hillingdon Ruislip
GospelOak
Mile End
Bow Road
Bromley-by-Bow
Upminster
Upminster Bridge
Hornchurch
Norwood Junction
Sydenham
Forest Hill
Anerley
Penge West
Honor Oak Park
Brockley
Harrow &Wealdstone
Cutty Sark for Maritime Greenwich
Ruislip Manor
Eastcote
Wapping
New Cross
Queens RoadPeckham
Peckham Rye
Denmark Hill
Surrey Quays
Whitechapel
Lewisham
Kilburn Park
Regent’s Park
KilburnHigh Road
EdgwareRoad
SouthHampstead
GoodgeStreet
Shepherd’s BushMarket
Goldhawk Road
Hammersmith
Bayswater
Warren Street
Aldgate
Euston
Farringdon
BarbicanRussellSquare
Kensington(Olympia)
MorningtonCrescent
High StreetKensington
Old Street
St. John’s Wood
Green Park
BakerStreet
NottingHill Gate
Victoria
AldgateEast
Mansion House
Temple
OxfordCircus
BondStreet
TowerHill
Westminster
PiccadillyCircus
CharingCross
Holborn
Tower Gateway
Monument
Moorgate
Leicester Square
London Bridge
St. Paul’s
Hyde Park Corner
Knightsbridge
StamfordBrook
RavenscourtPark
WestKensington
NorthActon
HollandPark
Marylebone
Angel
Queensway MarbleArch
SouthKensington
SloaneSquare
WandsworthRoad
Covent Garden
LiverpoolStreet
GreatPortland
Street
Bank
EastActon
ChanceryLane
LancasterGate
Warwick AvenueMaida Vale
Fenchurch Street
Paddington
BaronsCourt
GloucesterRoad St. James’s
Park
Latimer RoadLadbroke Grove
Royal Oak
Westbourne Park
Bermondsey
Rotherhithe
ShoreditchHigh Street
Dalston Junction
Haggerston
Hoxton
Wood Lane
Shepherd’sBush
WhiteCity
King’s CrossSt. Pancras
EustonSquareEdgware
Road
Southwark
Embankment
Stratford High Street
Abbey Road
Star Lane
Blackfriars
Waterloo
TottenhamCourt Road
Canonbury
Cannon Street
Shadwell
Earl’sCourt
NorthGreenwich
CanadaWater
Clapham North
Clapham Common
Clapham South
Balham
Tooting Bec
Tooting Broadway
Colliers Wood
South Wimbledon
MordenThis diagram is an evolution of the original design conceived in 1931 by Harry Beck
Correct at time of going to print, December 2013
MAYOR OF LONDON
Transport for London
Key to lines
Metropolitan
Victoria
CircleCentralBakerloo
DLR
London Overground
Piccadilly
Waterloo & City
Jubilee
Hammersmith & City
Northern
DistrictDistrict open weekends, public holidays and some Olympia events
Emirates Air Line
Check before you travelBank Waterloo & City line open between Bank and Waterloo 0621-0030 Mondays to Fridays and 0802-0030 Saturdays. Between Waterloo and Bank 0615-0030 Mondays to Fridays and 0800-0030 Saturdays. Closed Sundays and Public Holidays.---------------------------------------------------------------------------------Camden Town Sunday 1300-1730 open for interchange and exit only.---------------------------------------------------------------------------------Canary Wharf Step-free interchange between Underground, Canary Wharf DLR and Heron Quays DLR stations at street level.---------------------------------------------------------------------------------Cannon Street Open until 2100 Mondays to Fridays and 0730-1930 Saturdays. Closed Sundays.---------------------------------------------------------------------------------Embankment Bakerloo and Northern line trains will not stop at this station from early January 2014 until early November 2014. ---------------------------------------------------------------------------------Emirates Greenwich Peninsula and Emirates Royal DocksSpecial fares apply. Open 0700-2000 Mondays to Fridays, 0800-2000 Saturdays, 0900-2000 Sundays and 0800-2000 Public Holidays. Opening hours are extended by one hour in the evening after 1 April 2014 and may be extended on certain events days. Please check close to the time of travel. ---------------------------------------------------------------------------------Heron Quays Step-free interchange between Heron Quays and Canary Wharf Underground station at street level.---------------------------------------------------------------------------------Hounslow WestStep-free access for manual wheelchairs only.---------------------------------------------------------------------------------Kilburn No step-free access from late January 2014 until mid May 2014.---------------------------------------------------------------------------------Stanmore Step-free access via a steep ramp. ---------------------------------------------------------------------------------Turnham Green Served by Piccadilly line trains until 0650 Mondays to Saturdays, 0745 Sundays and after 2230 every evening. At other times use District line.---------------------------------------------------------------------------------Waterloo Waterloo & City line open between Bank and Waterloo 0621-0030 Mondays to Fridays and 0802-0030 Saturdays. Between Waterloo and Bank 0615-0030 Mondays to Fridays and 0800-0030 Saturdays. Closed Sundays and Public Holidays.No step-free access from late January 2014 until late July 2014.---------------------------------------------------------------------------------West India QuayNot served by DLR trains from Bank towards Lewisham before 2100 on Mondays to Fridays.---------------------------------------------------------------------------------
Tube map
London metro, London Transport
Protein interactions
Protein-protein interaktionsnetværk, Jeong et al, Nature Review | Genetics
Examples of applications of graphs
Graph Vertices Edges
communication computers, routers, etc. cables
transport intersections roads
transport airports flight routes
games position valid move
neural networks neuron Synapses
financial network stocks or currencies transactions
circuits logical gates connections
food chain species predator-prey
molecule atom bindings
• Undirected graph. G = (V, E)• V = set of vertices• E = set of edges (each edge is a pair of vertices)• n = |V|, m = |E|
• Path (da: sti). Sequence of vertices connected by edges.• Cycle (da: kreds). (Nonempty) path starting and ending at the same vertex.• Degree (da: grad). deg(v) = the number of neighbours of v, ore dges incident to v. • Connectivity (da: sammenhæng). A pair of vertices are connected if there is a path
between them
Terminology
0
7
9 10
1211
1
3
2
5
6 8
4
Path from 0 to 3
Cycle
deg(2) = 3V = {0,1,2,…,12}E = {(0,1), (0,2) (0,4),(2,3),…, (11,12)}
n = 13, m = 15
• Lemma. ∑v∈V
deg(v) = 2m.
• Proof. How many times is each edge counted in this sum?
Undirected graphs
0
7
9 10
1211
1
3
2
5
6 8
4
• Path. Is there a path connecting s and t?• Shortest path. What is the shortest path connecting s and t?• Longest path. What is the longest simple (ie. not self-intersecting) path connecting s
and t?
• Cycle. Is there a cycle in the graph?• Euler tour. Is there a cycle that uses each edge exactly once?• Hamilton cycle. Is there a cycle that visits each vertex exactly once?
• Connectivity. Is any pair of vertices connectable by a path? • Minimum spanning tree. What is the cheapest way of connecting all vertices?• Biconnectivity. Is there a vertex whose removal would cause the graph to be
disconnected?
• Planarity. Is it possible to draw the graph in the plane without edge crossings?• Graph isomorphism. Do these sets of vertices and edges represent the same graph?
Algoritmic problems on graphs
Introduction to graphs
• Undriected graphs• Representation • Depth first search
• Connected components• Breadth first search
• Bipartite graphs
• G graph with n vertices and m edges.• Representation. We need the following operations on graphs.
• ADJACENT(v, u): determine whether u and v are neighbours.• NEIGHBOURS(v): return all neighbours of v.• INSERT(v, u): add the edge (v, u) to G (unless it is already there).
Representation
0
7
9 10
1211
1
3
2
5
6 8
4
• Graph G with n vertices and m edges.• Adjacency matrix (incidensmatrix).
• 2D n ⨉ n table A.• A[i,j] = 1 if i and j er neighbours, 0 otherwise
• Complexity?• Space. O(n2)• Time.
• ADJACENT O(1) time• NEIGHBOURS(v) O(n) time.• INSERT(v, u) O(1) time.
Adjacency matrix representation
0 1 2 3 4 5 6 7 8 9 10 11 120 0 1 1 0 1 0 0 0 0 0 0 0 01 1 0 0 0 0 0 0 0 0 0 0 0 02 1 0 0 1 0 1 0 0 0 0 0 0 03 0 0 1 0 1 1 0 0 0 0 0 0 04 1 0 0 1 0 1 0 0 0 0 0 0 05 0 0 1 1 1 0 0 0 0 0 0 0 06 0 0 0 0 0 0 0 1 1 0 0 0 07 0 0 0 0 0 0 1 0 0 0 0 0 08 0 0 0 0 0 0 1 0 0 0 0 0 09 0 0 0 0 0 0 0 0 0 0 1 1 1
10 0 0 0 0 0 0 0 0 0 1 0 0 111 0 0 0 0 0 0 0 0 0 1 0 0 112 0 0 0 0 0 0 0 0 0 1 1 1 0
0
7
9 10
1211
1
3
2
5
6 8
4
• Graph G with n vertices and m edges.• Adjacency list (da: incidensliste).
• Tabel A[0..n-1].• A[i] contains a list of all neighbours to
i.• Complexity?
• Space. O(n + ∑v∈V
deg(v)) = O(n + m)
• Time.• ADJACENT, NEIGHBOURS, INSERT
O(deg(v)) time.
Adjacency list representation0
7
9 10
1211
1
3
2
5
6 8
4
0123456789
101112
1 2 400 3 52 4 50 3 52 3 47 866
10 11 129 129 129 10 11
• Real world graphs are often sparse.
Repræsentation
Datastruktur ADJACENT NEIGHBOURS INSERT Plads
adjacency matrix O(1) O(n) O(1) O(n2)
adjacency list O(deg(v)) O(deg(v)) O(deg(v)) O(n+m)
Introduction to graphs
• Undirected graphs• Representation • Depth first search
• Connected Components• Breadth first search
• Bipartite graphs
• Algorithm for systematically visiting all vertices and edges.• Depth first search (dybde-først) from vertex s.
• Initially, all vertices un-marked, and visit vertex s. • Visit vertex v:
• Mark v.• Visit all unmarked neighbours of v recursively.
• Intuition.• Explore out from s in some direction, until coming to a “dead end”. • Go back to the last place where there were unexplored edges. Repeat.
• Discovery time (starttid). First time a vertex is visited.• Finish time (sluttid). Last time a vertex is visited.
Depth first search
s
• Exercise. Run DFS (depth first search) from vertex 0, and report discovery time and finish time for each vertex. Assume the adjacency lists are sorted.
Depth first search
0
1
3
2
4
5
• Time. (assuming the graph is given in adjacency list representation)• Recursion? once per vertex.• O(deg(v)) time spent on vertex v.
• ⟹ total O(n + ∑v∈V
deg(v)) = O(n + m) time.
• Only visits vertices connected to s.
Depth first searchDFS(s)
time = 0DFS-VISIT(s)
DFS-VISIT(v)v.d = time++marker vforeach unmarked neighbour u
DFS-VISIT(u) u.π = v
v.f = time++
s
• Flood fill (farveudfyldning). Chance the colour of a connected area of green pixels.
• Algorithm. • Build a grid graph and run DFS (depth first search). • Vertex: pixel.• Edge: goes between neighbouring pixels of same colour.• Area: all vertices connected to a given vertex.
Flood fill• Definition. A connected component (sammenhængskomponent) is a maximal subset
of connected vertices.
• How does one find all connected components? • Algorithm.
• Initially, let all vertices be unmarked. • While there is an unmarked vertex:
• Chose an unmarked vertex v, run DFS from v. • Time. O(n + m).
Connected components
0
7
9 10
1211
1
3
2
5
6 8
4
Introduction to graphs
• Undirected graphs• Representation • Depth first search
• Connected Components• Breadth first search
• Bipartite graphs
• Breadth first search (breddeførst søgning) from s. • Initially, let all vertices be unmarked. • Mark s, and add s to the queue K. • While K is not empty:
• Excerpt vertex v from K.• For all unmarked neighbours u of v
• Mark u.• Add u to K.
• Intuition.• Explore, starting from s, in all directions - in increasing distance from s.
• Shortest paths from s. • Distance to s in BFS tree = shortest distance to s in the original graph.
Breadth first search
s
• Exercise. Run BFS from vertex 0 and indicate the shortest paths. Assume the adjacency lists are sorted.
Breadth first search
0
1
3
2
4
5
• Lemma. BFS finds the length of the shortest path from s to all other vertices.• Intuition.
• BFS assigns vertices to layers. Layer number i contains all vertices of distance i to s.
• What does each layer contain?• L0 : {s}• L1 : all neighbours of L0.• L2 : all neighbours if L1 that are not neighbours of L0
• L3 : all neighbours of L2 that neither are neighbours of L0 nor L1.• …• Li : all neighbours til Li-1 not neighbouring Lj for j < i-1
• = all vertices of distance i from s.
Shortest paths
s
L0
L1 L2
L3
• Time. (assuming adjacency list representation)• Each vertex is visited at most once.• O(deg(v)) time spent on vertex v.
• ⟹ total O(n + ∑v∈V
deg(v)) = O(n + m) time.
• Only vertices connected to s are visited.
Breadth first searchBFS(s)
mark ss.d = 0K.ENQUEUE(s)repeat until K is empty
v = K.DEQUEUE()foreach unmarked neighbour u
mark uu.d = v.d + 1 u.π = v K.ENQUEUE(u)
s Introduction to graphs
• Undirected graphs• Representation • Depth first search
• Connected Components• Breadth first search
• Bipartite graphs
• Definition. A graph is bipartite (todelt) if and only if all vertices can be coloured red and blue such that every edge has exactly one red and one blue endpoint.
• Alternativt definition. A graph is bipartite if and only if its vertices can be partitioned into two sets V1 and V2 such that all edges go between V1 and V2.
• Application. • Scheduling, matching, assigning customers to servers, assigning jobs to
machines, assigning students to advisors/labs, …• Many graph problems are easier on bipartite graphs.
Bipartite graphs• Challenge. Given a graph G, determine whether G is bipartite.
Bipartite graphs
1
6
5
3
2
7
4
1
6
5
3
2
7
4
• Lemma. A graph G is bipartite if and only if all cycles in G have even length.
• Proof. ⟹
• If G is bipartite, all cycles start and end in the same side.
Bipartite graphs• Lemma. A graph G is bipartite if and only if all cycles in G have even length.
• Proof. ⟸
• Choose a vertex v and consider BFS layers L0, L1, …, Lk.
• All cycles have even length ⟹ There is no edge between vertices of the same layer ⟹ We can assign alternating (red, blue) colours to the layers ⟹ G is bipartite.
Bipartite graphs
• Algorithm.• Run BFS on G.• For every edge of G, determine
whether it goes between vertices of the same layer.
• Time.• O(n + m)
Bipartite graphs
s
L0
L1 L2
L3
s
L0
L1 L2
L3
• All running times assume that G is given in the adjacency list representation.
Graph algorithmsAlgorithm TIme Space
Depth first search O(n + m) O(n + m)
Breadth first search O(n + m) O(n + m)
Connected components O(n + m) O(n + m)
Bipartite O(n + m) O(n + m)
Introduction to graphs
• Undirected graphs• Representation • Depth first search
• Connected components• Breadth first search
• Bipartite graphs