CSED233: Data Structures (2014F) Lecture17: Graph I Bohyung Han CSE, POSTECH [email protected] Graph β’ Definition ο§ A graph πΊ = π, πΈ consists of a finite set of vertices, π, and a finite set of edges, πΈ. β’ Properties ο§ π and πΈ are sets: each vertex π£ β π and each edge π β πΈ are unique. ο§ Edge: π’, π£ where π’, π£ β π ο§ Vertices and edges may store elements. 2 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Graphs β’ Example: ο§ A vertex represents an airport and stores the threeβletter airport code. ο§ An edge represents a flight route between two airports and stores the mileage of the route. PVD ORD SFO LGA LAX DFW HNL MIA 3 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Applications β’ Electronic circuits cslab1a cslab1b ο§ Printed circuit board ο§ Integrated circuit math.brown.edu β’ Transportation networks cs.brown.edu ο§ Highway network ο§ Flight network brown.edu β’ Computer networks qwest.net ο§ Local area network ο§ Internet ο§ Web att.net cox.net β’ Databases John ο§ Entityβrelationship diagram Paul 4 David CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Applications Gene network ImageNet 5 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Basic Definitions in Graph β’ Undirected graph ο§ Graph with undirected edges ο§ Undirected edges: π’, π£ = π£, π’ ORD 849 miles PVD ORD flight AA 1206 PVD β’ Directed graph (= digraph) ο§ Graph with directed edges ο§ Directed edges β’ Ordered pair of vertices, π’, π£ β π£, π’ β’ The first vertex is the origin and the second vertex is the destination. β’ Sparse graph ο§ Graph with few edges ο§ πΈ = π π , where β denotes the number of elements in a set β’ Dense graph ο§ Graph with many edges: πΈ = π π 6 2 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Undirected Graph π π π π π π π π π π π = {π’, π£, π€, π₯, π¦} π = {π’, π£, π€, π₯, π¦} πΈ = { π’, π£ , π£, π₯ , π’, π€ , π€, π₯ , π€, π¦ , π₯, π¦ } πΈ = { π’, π£ , π€, π₯ , π€, π¦ , π₯, π¦ } 7 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Directed Graph π π π π π π = {π’, π£, π€, π₯, π¦} πΈ = { π£, π’ , π€, π’ , π£, π₯ , π₯, π€ , π¦, π€ , π₯, π¦ , π¦, π₯ } 8 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Terminology β’ End vertices (or endpoints) of an edge ο§ π’ and π£ are the endpoints of ππ β’ Edges incident on a vertex ο§ ππ , ππ and ππ are incident on π£. π ππ β’ Adjacent vertices ο§ π’, π£ β πΈ ο§ Vertices π’ and π£ are adjacent. ππ ππ π ππ β’ Degree of a vertex π π ππ ππ π ο§ Number of incident edges ο§ deg π₯ = 5 ππ ππ ππ ππ β’ Selfβloop π ο§ Edge between the same vertex ο§ ππ is a selfβloop. 9 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Terminology (contβd) β’ Path ο§ Sequence of vertices π£1 , π£2 , β¦ , π£π such that π£π , π£π+1 β πΈ ο§ Length of a path β’ Number of edges on the path β’ The length of the path from a vertex to itself is 0. π ππ π π ππ π β’ Simple path ο§ Path such that all its vertices and edges are distinct ο§ Example: π1 = π£, π₯, π§ ο§ Nonβsimple path: π2 = π’, π€, π₯, π¦, π€, π£ 10 π π CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Terminology (contβd) β’ Cycle ο§ Sequence of vertices π£1 , π£2 , β¦ , π£π such that π£π , π£π+1 β πΈ and π£1 = π£π ο§ No backtracking π β’ Simple cycle π ο§ Cycle such that all its vertices and edges are distinct. ο§ Example: π1 = π£, π₯, π¦, π€, π’, π£ ο§ Nonβsimple cycle example: visiting the same node multiple times π1 = π’, π€, π₯, π¦, π€, π£, π’ 11 ππ π π ππ π π CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Terminology in Directed Graph β’ Adjacency ο§ For directed graphs, vertex π€ is adjacent to π£ if and only if π£, π€ β πΈ. β’ Degree π ο§ Indegree β’ Number of incoming edges β’ deg ππ π₯ = 2 π ο§ Outdegree π π π β’ Number of outgoing edges β’ deg ππ’π‘ π€ = 2 β’ Path and cycle in directed graph π ο§ Should consider the direction of edges β’ Directed Acyclic Graph (DAG): directed graph with no cycles 12 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Properties of Graphs β’ For undirected graph ο§ πΊ = (π, πΈ), where π = π and πΈ = π ο§ π£ deg(π£) = 2π β’ Each edge is counted twice. ο§ π β€ π(π β 1)/2 with no selfβloops and no multiple edges ο§ Each vertex has degree at most π β 1 π = 4, π = 6, deg(π£) = 3 β’ What is the bound for a directed graph? 13 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Connectivity β’ Connected graph ο§ There is a path from every vertex to every other vertex. β’ Connected component ο§ Maximal connected subgraph β’ Complete graph Connected graph ο§ There is an edge between every pair of vertices. Non connected graph with two connected components Complete graph 14 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Connectivity in Directed Graph β’ Strongly connected graph ο§ There is a path from every vertex to every other vertex. Strongly connected graph β’ Weakly connected graph ο§ There is a path from every vertex to every other vertex, disregarding the direction of the edges. Weakly connected graph 15 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Connectivity β’ Subgraph ο§ A graph π is a subgraph of πΊ if and only if ππ β ππΊ and πΈπ β πΈπΊ Subgraph β’ A spanning subgraph ο§ A subgraph that contains all the vertices of the original graph Spanning subgraph 16 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Trees and Forests β’ A (free) tree is an undirected graph T such that ο§ T is connected ο§ T has no cycles ο§ This definition of tree is different from the one of a rooted tree Tree β’ Forest ο§ A forest is an undirected graph without cycles ο§ The connected components of a forest are trees Forest 17 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 Spanning Trees and Forests β’ Spanning tree ο§ A spanning subgraph that is a tree ο§ Not unique unless the graph is a tree Spanning tree Graph β’ Spanning forest ο§ A spanning subgraph that is a forest 18 CSED233: Data Structures by Prof. Bohyung Han, Fall 2014 19
© Copyright 2026 Paperzz