introduction to graphscourses.compute.dtu.dk/.../introtographs1x1.pdfintroduction to graphs •...

32
Philip Bille Introduction to Graphs Undirected Graphs Representation Depth-First Search Connected Components Breadth-First Search Bipartite Graphs

Upload: others

Post on 05-Mar-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Philip Bille

Introduction to Graphs

• Undirected Graphs• Representation • Depth-First Search

• Connected Components• Breadth-First Search

• Bipartite Graphs

Page 2: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Introduction to Graphs

• Undirected Graphs• Representation • Depth-First Search

• Connected Components• Breadth-First Search

• Bipartite Graphs

Page 3: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Undirected graph. Set of vertices pairwise joined by edges.

• Why graphs?• Models many natural problems from many 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

Page 4: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Visualizing the Internet

http://en.wikipedia.org/wiki/Opte_Project

Page 5: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Visualizing Friendships on Facebook

"Visualizing friendships", Paul Butler

Page 6: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

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

Page 7: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Protein Interaction Networks

Protein-protein interaktionsnetværk, Jeong et al, Nature Review | Genetics

Page 8: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Applications of Graphs

Graph Vertices Edges

communication computers cables

transport intersections roads

transport airports flight routes

games position valid move

neural network neuron synapses

financial network stocks transactions

circuit logical gates connections

food chain species predator-prey

molecule atom bindings

Page 9: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• 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. Sequence of vertices connected by edges.• Cycle. Path starting and ending at the same vertex.• Degree. deg(v) = the number of neighbors of v, or edges incident to v. • Connectivity. 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

Page 10: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Lemma. ∑v∈V

deg(v) = 2m.

• Proof. How many times is each edge counted in the sum?

Undirected Graphs

0

7

9 10

1211

1

3

2

5

6 8

4

Page 11: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• 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 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 uses each vertex exactly once?

• Connectivity. Are all pairs of vertices connected?• Minimum spanning tree. What is the best 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 edges crossing?• Graph isomorphism. Do these sets of vertices and edges represent the same graph?

Algoritmic Problems on Graphs

Page 12: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Introduction to Graphs

• Undirected Graphs• Representation • Depth-First Search

• Connected Components• Breadth-First Search

• Bipartite Graphs

Page 13: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Graph G with n vertices and m edges.• Representation. We need the following operations on graphs.

• ADJACENT(v, u): determine if u and v are neighbors.• NEIGHBORS(v): return all neighbors 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

Page 14: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Graph G with n vertices and m edges.• Adjacency matrix.

• 2D n ⨉ n array A.• A[i,j] = 1 if i and j are neighbors, 0 otherwise

• Complexity?• Space. O(n2)• Time.

• ADJACENT and INSERT in O(1) time.• NEIGHBOURS in O(n) time.

Adjacency Matrix

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

Page 15: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Graph G with n vertices and m edges.• Adjacency list.

• Array A[0..n-1].• A[i] is a linked list of all neighbors of i.

• Complexity?

• Space. O(n + ∑v∈V

deg(v)) = O(n + m)

• Time.• ADJACENT, NEIGHBOURS, INSERT

O(deg(v)) time.

Adjacency List 0

7

9 10

1211

1

3

2

5

6 8

4

0123456789

101112

1 2 400 3 52 4 50 3 52 3 47 86610 11 129 129 129 10 11

Page 16: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Real world graphs are often sparse.

Repræsentation

Data structure ADJACENT NEIGHBOURS INSERT space

adjacency matrix O(1) O(n) O(1) O(n2)

adjacency list O(deg(v)) O(deg(v)) O(deg(v)) O(n+m)

Page 17: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Introduction to Graphs

• Undirected Graphs• Representation • Depth-First Search

• Connected Components• Breadth-First Search

• Bipartite Graphs

Page 18: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Algorithm for systematically visiting all vertices and edges.• Depth first search from vertex s.

• Unmark all vertices and visit s. • Visit vertex v:

• Mark v.• Visit all unmarked neighbours of v recursively.

• Intuition.• Explore from s in some direction, until we read dead end. • Backtrack to the last position with unexplored edges. • Repeat.

• Discovery time. First time a vertex is visited.• Finish time. Last time a vertex is visited.

Depth-First Search

s

Page 19: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Time. (on 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++mark vfor each unmarked neighbor u

DFS-VISIT(u) u.π = v

v.f = time++

s

Page 20: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Flood fill. Chance the color of a connected area of green pixels.

• Algorithm. • Build a grid graph and run DFS. • Vertex: pixel.• Edge: between neighboring pixels of same color.• Area: connected component

Flood Fill

Page 21: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Definition. A connected component is a maximal subset of connected vertices.

• How to find all connected components? • Algorithm.

• Unmark all vertices.• 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

Page 22: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Introduction to Graphs

• Undirected Graphs• Representation • Depth-First Search

• Connected Components• Breadth-First Search

• Bipartite Graphs

Page 23: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Breadth first search from s. • Unmark all vertices and initialize queue Q.• Mark s and Q.ENQUEUE(s). • While Q is not empty:

• v = Q.DEQUEUE().• For each unmarked neighbor u of v

• Mark u.• Q.ENQUEUE(u).

• 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

Page 24: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• 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

Page 25: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Time. (on 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 = 0Q.ENQUEUE(s)repeat until Q.ISEMPTY()

v = Q.DEQUEUE()for each unmarked neighbor u

mark uu.d = v.d + 1 u.π = v Q.ENQUEUE(u)

s

Page 26: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Definition. A graph is bipartite if and only if all vertices can be colored red and blue such that every edge has exactly one red endpoint and one blue endpoint.

• Equivalent 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 clients to servers, assigning jobs to machines,

assigning students to advisors/labs, …• Many graph problems are easier on bipartite graphs.

Bipartite Graphs

Page 27: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• 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

Page 28: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• 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 on the same side.

Bipartite Graphs

Page 29: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• 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

Page 30: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• Algorithm.• Run BFS on G.• For each edge in G, check if it's

endpoints are in the same layer.

• Time.• O(n + m)

Bipartite Graphs

s

L0

L1 L2

L3

s

L0

L1 L2

L3

Page 31: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

• All on 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)

Page 32: Introduction to Graphscourses.compute.dtu.dk/.../introtographs1x1.pdfIntroduction to Graphs • Undirected Graphs • Representation • Depth-First Search • Connected Components

Introduction to Graphs

• Undirected Graphs• Representation • Depth-First Search

• Connected Components• Breadth-First Search

• Bipartite Graphs