SlideShare a Scribd company logo
2
Lecture outline
 graph concepts
 vertices, edges, paths
 directed/undirected
 weighting of edges
 cycles and loops
 searching for paths within a graph
 depth-first search
 breadth-first search
 Dijkstra's algorithm
 implementing graphs
 using adjacency lists
 using an adjacency matrix
3
Graphs
 graph: a data structure containing
 a set of vertices V
 a set of edges E, where an edge
represents a connection between 2 vertices
 the graph at right:
 V = {a, b, c}
 E = {(a, b), (b, c), (c, a)}
 Assuming that a graph can only have one edge between a pair of vertices, what is the
maximum number of edges a graph can contain, relative to the size of the vertex set
V?
4
More terminology
 degree: number of edges touching a vertex
 example: W has degree 4
 what is the degree of X? of Z?
 adjacent vertices: connected
directly by an edge
XU
V
W
Z
Y
a
c
b
e
d
f
g
h
i
j
5
Paths
 path: a path from vertex A to B is a sequence of edges that can be
followed starting from A to reach B
 can be represented as vertices visited or edges taken
 example: path from V to Z: {b, h} or {V, X, Z}
 reachability: V1 is reachable
from V2 if a path exists
from V1 to V2
 connected graph: one in
which it's possible to reach
any node from any other
 is this graph connected?
P1
XU
V
W
Z
Y
a
c
b
e
d
f
g
hP2
6
Cycles
 cycle: path from one node back to itself
 example: {b, g, f, c, a} or {V, X, Y, W, U, V}
 loop: edge directly from node to itself
 many graphs don't allow loops
C1
XU
V
W
Z
Y
a
c
b
e
d
f
g
hC2
7
Weighted graphs
 weight: (optional) cost associated with a given edge
 example: graph of airline flights
 vertices: cities (airports) to which the airline flies
 edges: distance between airports in miles
 if we were programming this graph, what information would we have to store for each
vertex / edge?
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
8
Directed graphs
 directed graph (digraph): edges are one-way connections between
vertices
 if graph is directed, a vertex has a separate in/out degree
9
Graph questions
 Are the following graphs directed or not directed?
 Buddy graphs of instant messaging programs?
(vertices = users, edges = user being on another's buddy list)
 bus line graph depicting all of Seattle's bus stations and routes
 graph of the main backbone servers
on the internet
 graph of movies in which actors
have appeared together
 Are these graphs potentially cyclic?
Why or why not?
John
David
Paul
brown.edu
cox.net
cs.brown.edu
att.net
qwest.net
math.brown.edu
cslab1bcslab1a
10
Graph exercise
 Consider a graph of instant messenger buddies.
 What do the vertices represent? What does an edge represent?
 Is this graph directed or undirected? Weighted or unweighted?
 What does a vertex's degree mean? In degree? Out degree?
 Can the graph contain loops? cycles?
 Consider this graph data:
 Marty's buddy list: Mike, Sarah, Amanda.
 Mike's buddy list: Sarah, Emily.
 David's buddy list: Emily, Mike.
 Amanda's buddy list: Emily, Mike.
 Sarah's buddy list: Amanda, Marty.
 Emily's buddy list: Mike.
 Compute the in/out degree of each vertex. Is the graph connected?
 Who is the most popular? Least? Who is the most antisocial?
 If we're having a party and want to distribute the message the most quickly, who should we tell
first?
11
Depth-first search
 depth-first search (DFS): finds a path between two vertices by exploring
each possible path as many steps as possible before backtracking
 often implemented recursively
12
DFS pseudocode
 Pseudo-code for depth-first search:
dfs(v1, v2):
dfs(v1, v2, {})
dfs(v1, v2, path):
path += v1.
mark v1 as visited.
if v1 is v2:
path is found.
for each unvisited neighbor vi of v1
where there is an edge from v1 to vi:
if dfs(vi, v2, path) finds a path, path is found.
path -= v1. path is not found.
13
DFS example
 Paths tried from A to others (assumes ABC edge order)
 A
 A -> B
 A -> B -> D
 A -> B -> F
 A -> B -> F -> E
 A -> C
 A -> C -> G
 A -> E
 A -> E -> F
 A -> E -> F -> B
 A -> E -> F -> B -> D
 What paths would DFS return from D to each vertex?
14
DFS observations
 guaranteed to find a path if one exists
 easy to retrieve exactly what the path
is (to remember the sequence of edges
taken) if we find it
 optimality: not optimal. DFS is guaranteed to find a path, not necessarily
the best/shortest path
 Example: DFS(A, E) may return
A -> B -> F -> E
15
DFS example
 Using DFS, find a path from BOS to SFO.
JFK
BOS
MIA
ORD
LAX
DFW
SFO
v2
v1
v3
v4
v5
v6
v7
16
Breadth-first search
 breadth-first search (BFS): finds a path between two nodes by taking
one step down all paths and then immediately backtracking
 often implemented by maintaining
a list or queue of vertices to visit
 BFS always returns the path with
the fewest edges between the start
and the goal vertices
17
BFS pseudocode
 Pseudo-code for breadth-first search:
bfs(v1, v2):
List := {v1}.
mark v1 as visited.
while List not empty:
v := List.removeFirst().
if v is v2:
path is found.
for each unvisited neighbor vi of v
where there is an edge from v to vi:
List.addLast(vi).
path is not found.
18
BFS example
 Paths tried from A to others (assumes ABC edge order)
 A
 A -> B
 A -> C
 A -> E
 A -> B -> D
 A -> B -> F
 A -> C -> G
 A -> E -> F
 A -> B -> F -> E
 A -> E -> F -> B
 A -> E -> F -> B -> D
 What paths would BFS return from D to each vertex?
19
BFS observations
 optimality:
 in unweighted graphs, optimal. (fewest edges = best)
 In weighted graphs, not optimal.
(path with fewest edges might not have the lowest weight)
 disadvantage: harder to reconstruct what the actual path is once you find
it
 conceptually, BFS is exploring many possible paths in parallel, so it's not easy to store
a Path array/list in progress
 observation: any particular vertex is only part of one partial path at a time
 We can keep track of the path by storing predecessors for each vertex (references to
the previous vertex in that path)
20
BFS example
 Using BFS, find a path from BOS to SFO.
JFK
BOS
MIA
ORD
LAX
DFW
SFO
v2
v1
v3
v4
v5
v6
v7
21
DFS, BFS runtime
 What is the expected runtime of DFS, in terms of the number of vertices V
and the number of edges E ?
 What is the expected runtime of BFS, in terms of the number of vertices V
and the number of edges E ?
 Answer: O(|V| + |E|)
 each algorithm must potentially visit every node and/or examine every edge once.
 why not O(|V| * |E|) ?
 What is the space complexity of each algorithm?
22
Implementing graphs
23
Implementing a graph
 If we wanted to program an actual data structure to represent a graph,
what information would we need to store?
 for each vertex?
 for each edge?
 What kinds of questions
would we want to be able to
answer quickly:
 about a vertex?
 about its edges / neighbors?
 about paths?
 about what edges exist in the graph?
 We'll explore three common graph implementation strategies:
 edge list, adjacency list, adjacency matrix
1
2
3
4
5
6
7
24
Edge list
 edge list: an unordered list of all edges in the graph
 advantages
 easy to loop/iterate over all edges
 disadvantages
 hard to tell if an edge
exists from A to B
 hard to tell how many edges
a vertex touches (its degree)
1
2
5
1
1
6
2
7
2
3
3
4
7
4
5
6
5
7
5
4
1
2
3
4
5
6
7
25
Adjacency lists
 adjacency list: stores edges as individual linked lists of references to each
vertex's neighbors
 generally, no information needs to be stored in the edges, only in nodes, these arrays
can simply be pointers to other nodes and thus represent edges with little memory
requirement
26
Pros/cons of adjacency list
 advantage: new nodes can be added to the graph easily, and they can be connected with
existing nodes simply by adding elements to the appropriate arrays
 disadvantage: determining whether an edge exists between two nodes requires O(n) time,
where n is the average number of incident edges per node
27
Adjacency list example
 The graph at right has the following adjacency list:
 How do we figure out the degree of a given vertex?
 How do we find out whether an edge exists from A to B?
 How could we look for loops in the graph?
1
2
3
4
5
6
71
2
3
4
5
6
7
2 5 6
3 1 7
2 4
3 7 5
6 1 7 4
1 5
4 5 2
28
Adjacency matrix
 adjacency matrix: an n × n matrix where:
 the nondiagonal entry aij is the number of edges joining vertex i and vertex j (or the
weight of the edge joining vertex i and vertex j)
 the diagonal entry aii corresponds to the number of loops (self-connecting edges) at
vertex i
29
Pros/cons of Adj. matrix
 advantage: fast to tell whether edge exists between any two vertices i and
j (and to get its weight)
 disadvantage: consumes a lot of memory on sparse graphs (ones with few
edges)
30
Adjacency matrix example
 The graph at right has the following adjacency matrix:
 How do we figure out the degree of a given vertex?
 How do we find out whether an edge exists from A to B?
 How could we look for loops in the graph?
1
2
3
4
5
6
70
1
0
0
1
1
0
1
2
3
4
5
6
7
1
0
1
0
0
0
1
0
1
0
1
0
0
0
0
0
1
0
1
0
1
1
0
0
1
0
1
1
1
0
0
0
1
0
0
0
1
0
1
1
0
0
1 2 3 4 5 6 7
31
Runtime table
 n vertices, m edges
 no parallel edges
 no self-loops
Edge
List
Adjacency
List
Adjacency
Matrix
Space
Finding all adjacent
vertices to v
Determining if v is
adjacent to w
inserting a vertex
inserting an edge
removing vertex v
removing an edge
 n vertices, m edges
 no parallel edges
 no self-loops
Edge
List
Adjacency
List
Adjacency
Matrix
Space n + m n + m n2
Finding all adjacent
vertices to v
m deg(v) n
Determining if v is
adjacent to w
m
min(deg(v),
deg(w))
1
inserting a vertex 1 1 n2
inserting an edge 1 1 1
removing vertex v m deg(v) n2
removing an edge 1 deg(v) 1
32
0
1
0
1
2
3
1
0
1
0
1
0
0
0
1
1
0
0
1
0
0
0
1
0
1 2 3 4 5 6 7
Practical implementation
 Not all graphs have vertices/edges that are easily "numbered"
 how do we actually represent 'lists' or 'matrices' of vertex/edge relationships? How do we
quickly look up the edges and/or vertices adjacent to a given vertex?
 Adjacency list: Map<V, List<V>>
 Adjacency matrix: Map<V, Map<V, E>>
 Adjacency matrix: Map<V*V, E>
ORD
PVD
MIA
DFW
SFO
LAX
LGA
HNL
1
2
3
4
2 5 6
3 1 7
2 4
3 7 5
33
Maps and sets within graphs
since not all vertices can be numbered, we can use:
1. adjacency map
 each Vertex maps to a List of edges or adjacent Vertices
 Vertex --> List of Edges
 to get all edges adjacent to V1, look up
List<Edge> v1neighbors = map.get(V1)
2. adjacency adjacency matrix map
 each Vertex maps to a Hash of adjacent
 Vertex --> (Vertex --> Edge)
 to find out whether there's an edge from V1 to V2, call map.get(V1).containsKey(V2)
 to get the edge from V1 to V2, call map.get(V1).get(V2)

More Related Content

What's hot

Geom 1point2
Geom 1point2Geom 1point2
Geom 1point2herbison
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8ITNet
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data StructureProf Ansari
 
Isomorphic graph
Isomorphic graphIsomorphic graph
Isomorphic graphumair khan
 
2.1 Points, Lines, and Planes
2.1 Points, Lines, and Planes2.1 Points, Lines, and Planes
2.1 Points, Lines, and Planessmiller5
 
Application Of Graph Data Structure
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data StructureGaurang Dobariya
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data StructureKeno benti
 
1 1 understanding points, lines, & planes
1 1 understanding points, lines, & planes1 1 understanding points, lines, & planes
1 1 understanding points, lines, & planesjbm010203
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structuresSavit Chandra
 
1 4 segments, rays, parallel lines and planes
1 4 segments, rays, parallel lines and planes1 4 segments, rays, parallel lines and planes
1 4 segments, rays, parallel lines and planesHuron School District
 
Adjacency list
Adjacency listAdjacency list
Adjacency listStefi Yu
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphismCore Condor
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13sumitbardhan
 

What's hot (20)

Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Geom 1point2
Geom 1point2Geom 1point2
Geom 1point2
 
Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Graph therory
Graph theroryGraph therory
Graph therory
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Isomorphic graph
Isomorphic graphIsomorphic graph
Isomorphic graph
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
2.1 Points, Lines, and Planes
2.1 Points, Lines, and Planes2.1 Points, Lines, and Planes
2.1 Points, Lines, and Planes
 
Application Of Graph Data Structure
Application Of Graph Data StructureApplication Of Graph Data Structure
Application Of Graph Data Structure
 
Graphs
GraphsGraphs
Graphs
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
1 1 understanding points, lines, & planes
1 1 understanding points, lines, & planes1 1 understanding points, lines, & planes
1 1 understanding points, lines, & planes
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
1 4 segments, rays, parallel lines and planes
1 4 segments, rays, parallel lines and planes1 4 segments, rays, parallel lines and planes
1 4 segments, rays, parallel lines and planes
 
Adjacency list
Adjacency listAdjacency list
Adjacency list
 
Graph isomorphism
Graph isomorphismGraph isomorphism
Graph isomorphism
 
GEOMETRY: POINTS, LINES. PLANES
GEOMETRY: POINTS, LINES. PLANESGEOMETRY: POINTS, LINES. PLANES
GEOMETRY: POINTS, LINES. PLANES
 
358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13358 33 powerpoint-slides_13-graphs_chapter-13
358 33 powerpoint-slides_13-graphs_chapter-13
 

Viewers also liked

Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template LibraryAnirudh Raja
 
Discrete maths assignment
Discrete maths assignmentDiscrete maths assignment
Discrete maths assignmentKeshav Somani
 
Vertex Edge Graphs
Vertex Edge GraphsVertex Edge Graphs
Vertex Edge Graphsmrwilliams
 
Vertex edge graphs
Vertex edge graphsVertex edge graphs
Vertex edge graphsemteacher
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsAmrinder Arora
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphsmaznabili
 
Solving Problems with Graphs
Solving Problems with GraphsSolving Problems with Graphs
Solving Problems with GraphsMarko Rodriguez
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of GraphAbhishek Pachisia
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
The Graph Traversal Programming Pattern
The Graph Traversal Programming PatternThe Graph Traversal Programming Pattern
The Graph Traversal Programming PatternMarko Rodriguez
 
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...Marko Rodriguez
 

Viewers also liked (15)

Talk on Standard Template Library
Talk on Standard Template LibraryTalk on Standard Template Library
Talk on Standard Template Library
 
Discrete maths assignment
Discrete maths assignmentDiscrete maths assignment
Discrete maths assignment
 
Graph theory1234
Graph theory1234Graph theory1234
Graph theory1234
 
Vertex Edge Graphs
Vertex Edge GraphsVertex Edge Graphs
Vertex Edge Graphs
 
Vertex edge graphs
Vertex edge graphsVertex edge graphs
Vertex edge graphs
 
Graphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their RepresentationsGraphs, Trees, Paths and Their Representations
Graphs, Trees, Paths and Their Representations
 
Trees and graphs
Trees and graphsTrees and graphs
Trees and graphs
 
17 Trees and graphs
17 Trees and graphs17 Trees and graphs
17 Trees and graphs
 
Solving Problems with Graphs
Solving Problems with GraphsSolving Problems with Graphs
Solving Problems with Graphs
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Introduction to White box testing
Introduction to White box testingIntroduction to White box testing
Introduction to White box testing
 
The Graph Traversal Programming Pattern
The Graph Traversal Programming PatternThe Graph Traversal Programming Pattern
The Graph Traversal Programming Pattern
 
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
Problem-Solving using Graph Traversals: Searching, Scoring, Ranking, and Reco...
 
Graph theory
Graph theoryGraph theory
Graph theory
 

Similar to Talk on Graph Theory - I

Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First SearchNisha Soms
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxking779879
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.pptKarunaBiswas3
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gpchandrashekarr799
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptTalhaFarooqui12
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxRashidFaridChishti
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx pptDhruvilSTATUS
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfChristianKapsales1
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.pptFaruk Hossen
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 

Similar to Talk on Graph Theory - I (20)

09-graphs.ppt
09-graphs.ppt09-graphs.ppt
09-graphs.ppt
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
 
22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt22-graphs1-dfs-bfs.ppt
22-graphs1-dfs-bfs.ppt
 
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
22-graphs1-dfs-bfs.ppt odiehehei7hoh97ho7bi6vi6go7gp
 
Graphs
GraphsGraphs
Graphs
 
Spanningtreesppt
SpanningtreespptSpanningtreesppt
Spanningtreesppt
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
logic.pptx
logic.pptxlogic.pptx
logic.pptx
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptxData Structures and Agorithm: DS 21 Graph Theory.pptx
Data Structures and Agorithm: DS 21 Graph Theory.pptx
 
Graph
GraphGraph
Graph
 
DATA STRUCTURES.pptx
DATA STRUCTURES.pptxDATA STRUCTURES.pptx
DATA STRUCTURES.pptx
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.ppt
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 

Recently uploaded

IT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data AnalysisIT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data AnalysisDr. Radhey Shyam
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfPipe Restoration Solutions
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxR&R Consult
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageRCC Institute of Information Technology
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringC Sai Kiran
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdfKamal Acharya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdfKamal Acharya
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdfKamal Acharya
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfAyahmorsy
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdfAhmedHussein950959
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerapareshmondalnita
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopEmre Günaydın
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...Amil baba
 
Pharmacy management system project report..pdf
Pharmacy management system project report..pdfPharmacy management system project report..pdf
Pharmacy management system project report..pdfKamal Acharya
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamDr. Radhey Shyam
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxCenterEnamel
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxMd. Shahidul Islam Prodhan
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Aryaabh.arya
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdfKamal Acharya
 

Recently uploaded (20)

IT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data AnalysisIT-601 Lecture Notes-UNIT-2.pdf Data Analysis
IT-601 Lecture Notes-UNIT-2.pdf Data Analysis
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Scaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltageScaling in conventional MOSFET for constant electric field and constant voltage
Scaling in conventional MOSFET for constant electric field and constant voltage
 
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical EngineeringIntroduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
Introduction to Machine Learning Unit-4 Notes for II-II Mechanical Engineering
 
Toll tax management system project report..pdf
Toll tax management system project report..pdfToll tax management system project report..pdf
Toll tax management system project report..pdf
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 
Online blood donation management system project.pdf
Online blood donation management system project.pdfOnline blood donation management system project.pdf
Online blood donation management system project.pdf
 
Peek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdfPeek implant persentation - Copy (1).pdf
Peek implant persentation - Copy (1).pdf
 
ASME IX(9) 2007 Full Version .pdf
ASME IX(9)  2007 Full Version       .pdfASME IX(9)  2007 Full Version       .pdf
ASME IX(9) 2007 Full Version .pdf
 
fluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answerfluid mechanics gate notes . gate all pyqs answer
fluid mechanics gate notes . gate all pyqs answer
 
İTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering WorkshopİTÜ CAD and Reverse Engineering Workshop
İTÜ CAD and Reverse Engineering Workshop
 
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
NO1 Pandit Amil Baba In Bahawalpur, Sargodha, Sialkot, Sheikhupura, Rahim Yar...
 
Pharmacy management system project report..pdf
Pharmacy management system project report..pdfPharmacy management system project report..pdf
Pharmacy management system project report..pdf
 
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data StreamKIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
KIT-601 Lecture Notes-UNIT-3.pdf Mining Data Stream
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docxThe Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
The Ultimate Guide to External Floating Roofs for Oil Storage Tanks.docx
 
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptxCloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
Cloud-Computing_CSE311_Computer-Networking CSE GUB BD - Shahidul.pptx
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 

Talk on Graph Theory - I

  • 1.
  • 2. 2 Lecture outline  graph concepts  vertices, edges, paths  directed/undirected  weighting of edges  cycles and loops  searching for paths within a graph  depth-first search  breadth-first search  Dijkstra's algorithm  implementing graphs  using adjacency lists  using an adjacency matrix
  • 3. 3 Graphs  graph: a data structure containing  a set of vertices V  a set of edges E, where an edge represents a connection between 2 vertices  the graph at right:  V = {a, b, c}  E = {(a, b), (b, c), (c, a)}  Assuming that a graph can only have one edge between a pair of vertices, what is the maximum number of edges a graph can contain, relative to the size of the vertex set V?
  • 4. 4 More terminology  degree: number of edges touching a vertex  example: W has degree 4  what is the degree of X? of Z?  adjacent vertices: connected directly by an edge XU V W Z Y a c b e d f g h i j
  • 5. 5 Paths  path: a path from vertex A to B is a sequence of edges that can be followed starting from A to reach B  can be represented as vertices visited or edges taken  example: path from V to Z: {b, h} or {V, X, Z}  reachability: V1 is reachable from V2 if a path exists from V1 to V2  connected graph: one in which it's possible to reach any node from any other  is this graph connected? P1 XU V W Z Y a c b e d f g hP2
  • 6. 6 Cycles  cycle: path from one node back to itself  example: {b, g, f, c, a} or {V, X, Y, W, U, V}  loop: edge directly from node to itself  many graphs don't allow loops C1 XU V W Z Y a c b e d f g hC2
  • 7. 7 Weighted graphs  weight: (optional) cost associated with a given edge  example: graph of airline flights  vertices: cities (airports) to which the airline flies  edges: distance between airports in miles  if we were programming this graph, what information would we have to store for each vertex / edge? ORD PVD MIA DFW SFO LAX LGA HNL
  • 8. 8 Directed graphs  directed graph (digraph): edges are one-way connections between vertices  if graph is directed, a vertex has a separate in/out degree
  • 9. 9 Graph questions  Are the following graphs directed or not directed?  Buddy graphs of instant messaging programs? (vertices = users, edges = user being on another's buddy list)  bus line graph depicting all of Seattle's bus stations and routes  graph of the main backbone servers on the internet  graph of movies in which actors have appeared together  Are these graphs potentially cyclic? Why or why not? John David Paul brown.edu cox.net cs.brown.edu att.net qwest.net math.brown.edu cslab1bcslab1a
  • 10. 10 Graph exercise  Consider a graph of instant messenger buddies.  What do the vertices represent? What does an edge represent?  Is this graph directed or undirected? Weighted or unweighted?  What does a vertex's degree mean? In degree? Out degree?  Can the graph contain loops? cycles?  Consider this graph data:  Marty's buddy list: Mike, Sarah, Amanda.  Mike's buddy list: Sarah, Emily.  David's buddy list: Emily, Mike.  Amanda's buddy list: Emily, Mike.  Sarah's buddy list: Amanda, Marty.  Emily's buddy list: Mike.  Compute the in/out degree of each vertex. Is the graph connected?  Who is the most popular? Least? Who is the most antisocial?  If we're having a party and want to distribute the message the most quickly, who should we tell first?
  • 11. 11 Depth-first search  depth-first search (DFS): finds a path between two vertices by exploring each possible path as many steps as possible before backtracking  often implemented recursively
  • 12. 12 DFS pseudocode  Pseudo-code for depth-first search: dfs(v1, v2): dfs(v1, v2, {}) dfs(v1, v2, path): path += v1. mark v1 as visited. if v1 is v2: path is found. for each unvisited neighbor vi of v1 where there is an edge from v1 to vi: if dfs(vi, v2, path) finds a path, path is found. path -= v1. path is not found.
  • 13. 13 DFS example  Paths tried from A to others (assumes ABC edge order)  A  A -> B  A -> B -> D  A -> B -> F  A -> B -> F -> E  A -> C  A -> C -> G  A -> E  A -> E -> F  A -> E -> F -> B  A -> E -> F -> B -> D  What paths would DFS return from D to each vertex?
  • 14. 14 DFS observations  guaranteed to find a path if one exists  easy to retrieve exactly what the path is (to remember the sequence of edges taken) if we find it  optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path  Example: DFS(A, E) may return A -> B -> F -> E
  • 15. 15 DFS example  Using DFS, find a path from BOS to SFO. JFK BOS MIA ORD LAX DFW SFO v2 v1 v3 v4 v5 v6 v7
  • 16. 16 Breadth-first search  breadth-first search (BFS): finds a path between two nodes by taking one step down all paths and then immediately backtracking  often implemented by maintaining a list or queue of vertices to visit  BFS always returns the path with the fewest edges between the start and the goal vertices
  • 17. 17 BFS pseudocode  Pseudo-code for breadth-first search: bfs(v1, v2): List := {v1}. mark v1 as visited. while List not empty: v := List.removeFirst(). if v is v2: path is found. for each unvisited neighbor vi of v where there is an edge from v to vi: List.addLast(vi). path is not found.
  • 18. 18 BFS example  Paths tried from A to others (assumes ABC edge order)  A  A -> B  A -> C  A -> E  A -> B -> D  A -> B -> F  A -> C -> G  A -> E -> F  A -> B -> F -> E  A -> E -> F -> B  A -> E -> F -> B -> D  What paths would BFS return from D to each vertex?
  • 19. 19 BFS observations  optimality:  in unweighted graphs, optimal. (fewest edges = best)  In weighted graphs, not optimal. (path with fewest edges might not have the lowest weight)  disadvantage: harder to reconstruct what the actual path is once you find it  conceptually, BFS is exploring many possible paths in parallel, so it's not easy to store a Path array/list in progress  observation: any particular vertex is only part of one partial path at a time  We can keep track of the path by storing predecessors for each vertex (references to the previous vertex in that path)
  • 20. 20 BFS example  Using BFS, find a path from BOS to SFO. JFK BOS MIA ORD LAX DFW SFO v2 v1 v3 v4 v5 v6 v7
  • 21. 21 DFS, BFS runtime  What is the expected runtime of DFS, in terms of the number of vertices V and the number of edges E ?  What is the expected runtime of BFS, in terms of the number of vertices V and the number of edges E ?  Answer: O(|V| + |E|)  each algorithm must potentially visit every node and/or examine every edge once.  why not O(|V| * |E|) ?  What is the space complexity of each algorithm?
  • 23. 23 Implementing a graph  If we wanted to program an actual data structure to represent a graph, what information would we need to store?  for each vertex?  for each edge?  What kinds of questions would we want to be able to answer quickly:  about a vertex?  about its edges / neighbors?  about paths?  about what edges exist in the graph?  We'll explore three common graph implementation strategies:  edge list, adjacency list, adjacency matrix 1 2 3 4 5 6 7
  • 24. 24 Edge list  edge list: an unordered list of all edges in the graph  advantages  easy to loop/iterate over all edges  disadvantages  hard to tell if an edge exists from A to B  hard to tell how many edges a vertex touches (its degree) 1 2 5 1 1 6 2 7 2 3 3 4 7 4 5 6 5 7 5 4 1 2 3 4 5 6 7
  • 25. 25 Adjacency lists  adjacency list: stores edges as individual linked lists of references to each vertex's neighbors  generally, no information needs to be stored in the edges, only in nodes, these arrays can simply be pointers to other nodes and thus represent edges with little memory requirement
  • 26. 26 Pros/cons of adjacency list  advantage: new nodes can be added to the graph easily, and they can be connected with existing nodes simply by adding elements to the appropriate arrays  disadvantage: determining whether an edge exists between two nodes requires O(n) time, where n is the average number of incident edges per node
  • 27. 27 Adjacency list example  The graph at right has the following adjacency list:  How do we figure out the degree of a given vertex?  How do we find out whether an edge exists from A to B?  How could we look for loops in the graph? 1 2 3 4 5 6 71 2 3 4 5 6 7 2 5 6 3 1 7 2 4 3 7 5 6 1 7 4 1 5 4 5 2
  • 28. 28 Adjacency matrix  adjacency matrix: an n × n matrix where:  the nondiagonal entry aij is the number of edges joining vertex i and vertex j (or the weight of the edge joining vertex i and vertex j)  the diagonal entry aii corresponds to the number of loops (self-connecting edges) at vertex i
  • 29. 29 Pros/cons of Adj. matrix  advantage: fast to tell whether edge exists between any two vertices i and j (and to get its weight)  disadvantage: consumes a lot of memory on sparse graphs (ones with few edges)
  • 30. 30 Adjacency matrix example  The graph at right has the following adjacency matrix:  How do we figure out the degree of a given vertex?  How do we find out whether an edge exists from A to B?  How could we look for loops in the graph? 1 2 3 4 5 6 70 1 0 0 1 1 0 1 2 3 4 5 6 7 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 1 0 1 1 0 0 1 0 1 1 1 0 0 0 1 0 0 0 1 0 1 1 0 0 1 2 3 4 5 6 7
  • 31. 31 Runtime table  n vertices, m edges  no parallel edges  no self-loops Edge List Adjacency List Adjacency Matrix Space Finding all adjacent vertices to v Determining if v is adjacent to w inserting a vertex inserting an edge removing vertex v removing an edge  n vertices, m edges  no parallel edges  no self-loops Edge List Adjacency List Adjacency Matrix Space n + m n + m n2 Finding all adjacent vertices to v m deg(v) n Determining if v is adjacent to w m min(deg(v), deg(w)) 1 inserting a vertex 1 1 n2 inserting an edge 1 1 1 removing vertex v m deg(v) n2 removing an edge 1 deg(v) 1
  • 32. 32 0 1 0 1 2 3 1 0 1 0 1 0 0 0 1 1 0 0 1 0 0 0 1 0 1 2 3 4 5 6 7 Practical implementation  Not all graphs have vertices/edges that are easily "numbered"  how do we actually represent 'lists' or 'matrices' of vertex/edge relationships? How do we quickly look up the edges and/or vertices adjacent to a given vertex?  Adjacency list: Map<V, List<V>>  Adjacency matrix: Map<V, Map<V, E>>  Adjacency matrix: Map<V*V, E> ORD PVD MIA DFW SFO LAX LGA HNL 1 2 3 4 2 5 6 3 1 7 2 4 3 7 5
  • 33. 33 Maps and sets within graphs since not all vertices can be numbered, we can use: 1. adjacency map  each Vertex maps to a List of edges or adjacent Vertices  Vertex --> List of Edges  to get all edges adjacent to V1, look up List<Edge> v1neighbors = map.get(V1) 2. adjacency adjacency matrix map  each Vertex maps to a Hash of adjacent  Vertex --> (Vertex --> Edge)  to find out whether there's an edge from V1 to V2, call map.get(V1).containsKey(V2)  to get the edge from V1 to V2, call map.get(V1).get(V2)