SlideShare a Scribd company logo
1 of 18
DATA STRUCTURES:
GRAPHS
1
GRAPHS
Graphs = formalism for representing relationships between objects
• A graph G = (V, E)
• V is a set of vertices
• E  V  V is a set of edges
2
Han
Leia
Luke
V = {Han, Leia, Luke}
E = {(Luke, Leia),
(Han, Leia),
(Leia, Han)}
DIRECTED VS. UNDIRECTED
GRAPHS
• In directed graphs, edges have a specific direction:
• In undirected graphs, they don’t (edges are two-way):
• Vertices u and v are adjacent if (u, v)  E
3
Han
Leia
Luke
Han
Leia
Luke
WEIGHTED GRAPHS
4
20
30
35
60
Mukilteo
Edmonds
Seattle
Bremerton
Bainbridge
Kingston
Clinton
There may be more
information in the graph as well.
Each edge has an associated weight or cost.
PATHS AND CYCLES
A path is a list of vertices {v1, v2, …, vn} such
that (vi, vi+1)  E for all 0  i < n.
A cycle is a path that begins and ends at the
same node.
5
Seattle
San Francisco
Dallas
Chicago
Salt Lake City
p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco,
PATH LENGTH AND COST
Path length: the number of edges in the path
Path cost: the sum of the costs of each edge
6
Seattle
San Francisco
Dallas
Chicago
Salt Lake City
3.5
2 2
2.5
3
2
2.5
2.5
length(p) = 5 cost(p) = 11.5
CONNECTIVITY
Undirected graphs are connected if there is a path between any
two vertices
Directed graphs are strongly connected if there is a path from
any one vertex to any other
7
CONNECTIVITY
Directed graphs are weakly connected if there is a path between
any two vertices, ignoring direction
A complete graph has an edge between every pair of vertices
8
CONNECTIVITY
A (strongly) connected component is a subgraph which is
(strongly) connected
CC in an undirected graph:
SCC in a directed graph:
9
DISTANCE AND DIAMETER
• The distance between two nodes, d(u,v), is the
length of the shortest paths, or  if there is no path
• The diameter of a graph is the largest distance
between any two nodes
• Graph is strongly connected iff diameter < 
10
OTHER GRAPHS
• Geography:
• Cities and roads
• Airports and flights (diameter  20 !!)
• Publications:
• The co-authorship graph
• E.g. the Erdos distance
• The reference graph
• Phone calls: who calls whom
• Almost everything can be modeled as a graph !
11
GRAPH REPRESENTATION 1:
ADJACENCY MATRIX
A |V| x |V| array in which an element (u, v) is
true if and only if there is an edge from u to v
12
Han
Leia
Luke
Han Luke Leia
Han
Luke
LeiaRuntime:
iterate over vertices
iterate ever edges
iterate edges adj. to vertex
edge exists?
Space requirements:
GRAPH REPRESENTATION 2:
ADJACENCY LIST
A |V|-ary list (array) in which each entry stores a list (linked
list) of all adjacent vertices
13
Han
Leia
Luke
Han
Luke
Leia
space requirements:
Runtime:
iterate over vertices
iterate ever edges
iterate edges adj. to vertex
edge exists?
TREES AS GRAPHS
• Every tree is a graph with some
restrictions:
• the tree is directed
• there are no cycles (directed or
undirected)
• there is a directed path from the
root to every node
14
A
B
D E
C
F
HG
JI
BAD!
DIRECTED ACYCLIC GRAPHS
(DAGS)
DAGs are directed graphs
with no cycles.
15
main()
add()
access()
mult()
read()
Trees  DAGs  Graphs
if program
call graph is a
DAG, then all
procedure
calls can be
in-lined
APPLICATION OF DAGS:
REPRESENTING PARTIAL ORDERS
16
check in
airport
call
taxi
taxi to
airport
reserve
flight
pack
bagstake
flight
locate
gate
TOPOLOGICAL SORT
Given a graph, G = (V, E), output all the vertices in V
such that no vertex is output before any other vertex with an
edge to it.
17
check in
airport
call
taxi
taxi to
airport
reserve
flight
pack
bags
take
flight
locate
gate
TOPOLOGICAL SORT
18
check in
airport
call
taxi
taxi to
airport
reserve
flight
pack
bags
take
flight
locate
gate

More Related Content

What's hot

What's hot (20)

Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graphs in Data Structure
 Graphs in Data Structure Graphs in Data Structure
Graphs in Data Structure
 
Data Structure : Graph and Graph Traversing
Data Structure : Graph and Graph TraversingData Structure : Graph and Graph Traversing
Data Structure : Graph and Graph Traversing
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 
Graph
GraphGraph
Graph
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graph (Data structure)
Graph (Data structure) Graph (Data structure)
Graph (Data structure)
 
Graph Data Structure
Graph Data StructureGraph Data Structure
Graph Data Structure
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
 
Skiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strcturesSkiena algorithm 2007 lecture10 graph data strctures
Skiena algorithm 2007 lecture10 graph data strctures
 
Graphs in data structure
Graphs in data structureGraphs in data structure
Graphs in data structure
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphs
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 

Similar to Learn Graph Data Structures and Algorithms

Similar to Learn Graph Data Structures and Algorithms (20)

Tn 110 lecture 8
Tn 110 lecture 8Tn 110 lecture 8
Tn 110 lecture 8
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
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
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
8-Graph.ppt
8-Graph.ppt8-Graph.ppt
8-Graph.ppt
 
Graphs.pptx
Graphs.pptxGraphs.pptx
Graphs.pptx
 
Data structure graphs
Data structure  graphsData structure  graphs
Data structure graphs
 
DATA STRUCTURE PRESENTATION.pptx
DATA STRUCTURE  PRESENTATION.pptxDATA STRUCTURE  PRESENTATION.pptx
DATA STRUCTURE PRESENTATION.pptx
 
Graph 1
Graph 1Graph 1
Graph 1
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
Graphs
GraphsGraphs
Graphs
 
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
 
gsm nithya.pdf
gsm nithya.pdfgsm nithya.pdf
gsm nithya.pdf
 
LEC 1.pptx
LEC 1.pptxLEC 1.pptx
LEC 1.pptx
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.ppt
 
Magtibay buk bind#2
Magtibay buk bind#2Magtibay buk bind#2
Magtibay buk bind#2
 
Graph
GraphGraph
Graph
 
Data structure
Data structureData structure
Data structure
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 

More from fika sweety

Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performancefika sweety
 
Program design techniques
Program design techniquesProgram design techniques
Program design techniquesfika sweety
 
Modeling and simulation ch 1
Modeling and simulation ch 1Modeling and simulation ch 1
Modeling and simulation ch 1fika sweety
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentationfika sweety
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchartfika sweety
 
Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02fika sweety
 
Coal presentationt
Coal presentationtCoal presentationt
Coal presentationtfika sweety
 
1 Computer Architecture
1 Computer Architecture1 Computer Architecture
1 Computer Architecturefika sweety
 
Warehouse chapter3
Warehouse chapter3   Warehouse chapter3
Warehouse chapter3 fika sweety
 
Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performancefika sweety
 
Database security copy
Database security   copyDatabase security   copy
Database security copyfika sweety
 

More from fika sweety (20)

Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performance
 
Program design techniques
Program design techniquesProgram design techniques
Program design techniques
 
Plsql
PlsqlPlsql
Plsql
 
Shift rotate
Shift rotateShift rotate
Shift rotate
 
Modeling and simulation ch 1
Modeling and simulation ch 1Modeling and simulation ch 1
Modeling and simulation ch 1
 
Macros...presentation
Macros...presentationMacros...presentation
Macros...presentation
 
Pseudocode algorithim flowchart
Pseudocode algorithim flowchartPseudocode algorithim flowchart
Pseudocode algorithim flowchart
 
Diversity (HRM)
Diversity (HRM)Diversity (HRM)
Diversity (HRM)
 
Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02Howtowriteamemo 090920105907-phpapp02
Howtowriteamemo 090920105907-phpapp02
 
Coal presentationt
Coal presentationtCoal presentationt
Coal presentationt
 
1 Computer Architecture
1 Computer Architecture1 Computer Architecture
1 Computer Architecture
 
3 Pipelining
3 Pipelining3 Pipelining
3 Pipelining
 
19 primkruskal
19 primkruskal19 primkruskal
19 primkruskal
 
Warehouse chapter3
Warehouse chapter3   Warehouse chapter3
Warehouse chapter3
 
Storage memory
Storage memoryStorage memory
Storage memory
 
Quick sort
Quick sortQuick sort
Quick sort
 
Query optimization and performance
Query optimization and performanceQuery optimization and performance
Query optimization and performance
 
L2
L2L2
L2
 
Master theorem
Master theoremMaster theorem
Master theorem
 
Database security copy
Database security   copyDatabase security   copy
Database security copy
 

Recently uploaded

Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 

Recently uploaded (20)

Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 

Learn Graph Data Structures and Algorithms

  • 2. GRAPHS Graphs = formalism for representing relationships between objects • A graph G = (V, E) • V is a set of vertices • E  V  V is a set of edges 2 Han Leia Luke V = {Han, Leia, Luke} E = {(Luke, Leia), (Han, Leia), (Leia, Han)}
  • 3. DIRECTED VS. UNDIRECTED GRAPHS • In directed graphs, edges have a specific direction: • In undirected graphs, they don’t (edges are two-way): • Vertices u and v are adjacent if (u, v)  E 3 Han Leia Luke Han Leia Luke
  • 4. WEIGHTED GRAPHS 4 20 30 35 60 Mukilteo Edmonds Seattle Bremerton Bainbridge Kingston Clinton There may be more information in the graph as well. Each edge has an associated weight or cost.
  • 5. PATHS AND CYCLES A path is a list of vertices {v1, v2, …, vn} such that (vi, vi+1)  E for all 0  i < n. A cycle is a path that begins and ends at the same node. 5 Seattle San Francisco Dallas Chicago Salt Lake City p = {Seattle, Salt Lake City, Chicago, Dallas, San Francisco,
  • 6. PATH LENGTH AND COST Path length: the number of edges in the path Path cost: the sum of the costs of each edge 6 Seattle San Francisco Dallas Chicago Salt Lake City 3.5 2 2 2.5 3 2 2.5 2.5 length(p) = 5 cost(p) = 11.5
  • 7. CONNECTIVITY Undirected graphs are connected if there is a path between any two vertices Directed graphs are strongly connected if there is a path from any one vertex to any other 7
  • 8. CONNECTIVITY Directed graphs are weakly connected if there is a path between any two vertices, ignoring direction A complete graph has an edge between every pair of vertices 8
  • 9. CONNECTIVITY A (strongly) connected component is a subgraph which is (strongly) connected CC in an undirected graph: SCC in a directed graph: 9
  • 10. DISTANCE AND DIAMETER • The distance between two nodes, d(u,v), is the length of the shortest paths, or  if there is no path • The diameter of a graph is the largest distance between any two nodes • Graph is strongly connected iff diameter <  10
  • 11. OTHER GRAPHS • Geography: • Cities and roads • Airports and flights (diameter  20 !!) • Publications: • The co-authorship graph • E.g. the Erdos distance • The reference graph • Phone calls: who calls whom • Almost everything can be modeled as a graph ! 11
  • 12. GRAPH REPRESENTATION 1: ADJACENCY MATRIX A |V| x |V| array in which an element (u, v) is true if and only if there is an edge from u to v 12 Han Leia Luke Han Luke Leia Han Luke LeiaRuntime: iterate over vertices iterate ever edges iterate edges adj. to vertex edge exists? Space requirements:
  • 13. GRAPH REPRESENTATION 2: ADJACENCY LIST A |V|-ary list (array) in which each entry stores a list (linked list) of all adjacent vertices 13 Han Leia Luke Han Luke Leia space requirements: Runtime: iterate over vertices iterate ever edges iterate edges adj. to vertex edge exists?
  • 14. TREES AS GRAPHS • Every tree is a graph with some restrictions: • the tree is directed • there are no cycles (directed or undirected) • there is a directed path from the root to every node 14 A B D E C F HG JI BAD!
  • 15. DIRECTED ACYCLIC GRAPHS (DAGS) DAGs are directed graphs with no cycles. 15 main() add() access() mult() read() Trees  DAGs  Graphs if program call graph is a DAG, then all procedure calls can be in-lined
  • 16. APPLICATION OF DAGS: REPRESENTING PARTIAL ORDERS 16 check in airport call taxi taxi to airport reserve flight pack bagstake flight locate gate
  • 17. TOPOLOGICAL SORT Given a graph, G = (V, E), output all the vertices in V such that no vertex is output before any other vertex with an edge to it. 17 check in airport call taxi taxi to airport reserve flight pack bags take flight locate gate
  • 18. TOPOLOGICAL SORT 18 check in airport call taxi taxi to airport reserve flight pack bags take flight locate gate