Graphs CSE 2011 Winter 2011 262022 1 Graphs
- Slides: 24
Graphs CSE 2011 Winter 2011 2/6/2022 1
Graphs l A graph is a pair (V, E), where ¡ V is a set of nodes, called vertices ¡ E is a collection of pairs of vertices, called edges ¡ Vertices and edges are objects and store elements l 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 337 HNL 2555 LAX 3 4 17 1233 849 ORD 802 SFO 1843 DFW 2 PVD 14 7 8 3 1 LGA 1120 10 99 MIA 2
Edge Types l Directed edge ¡ ¡ ordered pair of vertices (u, v) first vertex u is the origin second vertex v is the destination e. g. , a flight ORD flight AA 1206 PVD ORD 849 miles PVD l Undirected edge ¡ unordered pair of vertices (u, v) ¡ e. g. , a flight route l Directed graph (digraph) ¡ all the edges are directed ¡ e. g. , flight network l Undirected graph ¡ all the edges are undirected ¡ e. g. , route network l Mixed graph: contains both directed and undirected edges 3
Applications l Electronic circuits ¡ Printed circuit board ¡ Integrated circuit l Transportation networks ¡ Highway network ¡ Flight network l Computer networks ¡ Local area network ¡ Internet ¡ Web l Databases ¡ Entity-relationship diagram 4
Terminology l End vertices (or endpoints) of an edge ¡ U and V are the endpoints of a l Edges incident on a vertex ¡ a, d, and b are incident on V l Adjacent vertices ¡ U and V are adjacent l Degree of a vertex ¡ W has degree 4 l Loop ¡ j is a loop (we will consider only loopless graphs) a V b d U h X c e W j Z i g f Y 5
Terminology (2) For directed graphs: l Origin, destination of an edge l Outgoing edge l Incoming edge l Out-degree of vertex v: number of outgoing edges of v l In-degree of vertex v: number of incoming edges of v A B C D 6
Paths l Path ¡ sequence of alternating vertices and edges ¡ begins with a vertex ¡ ends with a vertex ¡ each edge is preceded and followed by its endpoints l Path length ¡ the total number of edges on the path l Simple path ¡ path such that all vertices are distinct (except that the first and last could be the same) l Examples ¡ P 1=(V, b, X, h, Z) is a simple path ¡ P 2=(U, c, W, e, X, g, Y, f, W, d, V) is a path that is not simple a U c V b d P 2 P 1 X e W h Z g f Y 7
Properties of Undirected Graphs Property 1 v deg(v) = 2 E Proof: each edge is counted twice Property 2 In an undirected graph with no loops E V (V - 1)/2 Proof: each vertex has degree at most (V - 1) Notation V number of vertices E number of edges deg(v) degree of vertex v Example ¡V = 4 ¡E = 6 ¡ deg(v) = 3 What is the bound for a directed graph? 8
Cycles l Cycle ¡ circular sequence of alternating vertices and edges ¡ each edge is preceded and followed by its endpoints l Simple cycle ¡ cycle such that all its vertices are distinct (except the first and the last) l Examples ¡ C 1=(V, b, X, g, Y, f, W, c, U, a, V) is a simple cycle ¡ C 2=(U, c, W, e, X, g, Y, f, W, d, V, a, U) is a cycle that is not simple l A directed graph is acyclic if it has no cycles called DAG (directed acyclic graph) a U c V b d C 2 X e C 1 g W f h Z Y 9
Connectivity Undirected Graphs connected not connected l An undirected graph is connected if there is a path from every vertex to every other vertex. 10
Connectivity Directed Graphs l A directed graph is called strongly connected if there is a path from every vertex to every other vertex. l If a directed graph is not strongly connected, but the corresponding undirected graph is connected, then the directed graph is said to be weakly connected. 11
Graph ADT and Data Structures CSE 2011 12
Representation of Graphs l Two popular computer representations of a graph: Both represent the vertex set and the edge set, but in different ways. 1. Adjacency Matrices Use a 2 D matrix to represent the graph 2. Adjacency Lists Use a set of linked lists, one list per vertex 13
Adjacency Matrix Representation l 2 D array of size n x n where n is the number of vertices in the graph l A[i][j]=1 if there is an edge connecting vertices i and j; otherwise, A[i][j]=0 14
Adjacency Matrix Example 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 1 0 8 1 0 0 1 1 0 0 0 1 2 2 0 1 0 0 0 1 0 9 3 0 1 0 0 1 3 4 4 0 0 1 1 0 0 0 7 6 5 5 0 0 0 1 0 0 0 6 0 0 0 1 0 0 7 0 1 0 0 0 8 1 0 0 0 0 1 9 0 1 0 0 0 1 0 15
Adjacency Matrices: Analysis l The storage requirement is (V 2). ¡not efficient if the graph has few edges. ¡appropriate if the graph is dense; that is E= (V 2) l If the graph is undirected, the matrix is symmetric. There exist methods to store a symmetric matrix using only half of the space. ¡ Note: the space requirement is still (V 2). l We can detect in O(1) time whether two vertices are connected. 16
Adjacency Lists l If the graph is sparse, a better solution is an adjacency list representation. l For each vertex v in the graph, we keep a list of vertices adjacent to v. 17
Adjacency List Example 0 8 2 9 1 7 3 4 6 5 0 8 1 2 3 7 9 2 1 4 8 3 1 4 5 4 2 3 5 3 6 6 5 7 7 1 6 8 0 2 9 9 1 8 18
Adjacency Lists: Analysis Space = (V + v deg(v)) = (V + E) l Testing whether u is adjacency to v takes time O(deg(v)) or O(deg(u)). 19
Adjacency Lists vs. Adjacency Matrices l An adjacency list takes (V + E). ¡ If E = O(V 2) (dense graph), both use (V 2) space. ¡ If E = O(V) (sparse graph), adjacency lists are more space efficient. l Adjacency lists ¡ More compact than adjacency matrices if graph has few edges ¡ Requires more time to find if an edge exists l Adjacency matrices ¡ Always require (V 2) space l This can waste lots of space if the number of edges is small ¡ Can quickly find if an edge exists 20
(Undirected) Graph ADT l Vertices and edges ¡ are positions ¡ store elements l Define Vertex and Edge interfaces, each extending Position interface l Accessor methods ¡ end. Vertices(e): an array of the two endvertices of e ¡ opposite(v, e): the vertex opposite of v on e ¡ are. Adjacent(v, w): true iff v and w are adjacent ¡ replace(v, x): replace element at vertex v with x ¡ replace(e, x): replace element at edge e with x l Update methods ¡ insert. Vertex(o): insert a vertex storing element o ¡ insert. Edge(v, w, o): insert an edge (v, w) storing element o ¡ remove. Vertex(v): remove vertex v (and its incident edges) ¡ remove. Edge(e): remove edge e l Iterator methods ¡ incident. Edges(v): edges incident to v ¡ vertices(): all vertices in the graph ¡ edges(): all edges in the graph 21
Homework l Prove the big-Oh running time of the graph methods shown in the next slide. 22
Running Time of Graph Methods • n vertices, m edges • no parallel edges • no self-loops • bounds are “big-Oh” Edge List Adjacency Matrix Space n+m n 2 incident. Edges(v) are. Adjacent (v, w) insert. Vertex(o) m m 1 deg(v) min(deg(v), deg(w)) 1 n 2 insert. Edge(v, w, o) 1 1 1 remove. Vertex(v) remove. Edge(e) m 1 deg(v) 1 n 2 1 23
Next Lectures l Graph traversal ¡Breadth first search (BFS) l. Applications of BFS ¡Depth first search (DFS) l Review l Final exam 24
- Winter kommt winter kommt flocken fallen nieder
- Winter kommt flocken fallen nieder
- Meine lieblingsjahreszeit ist der winter
- State graphs in software testing
- Comparing distance/time graphs to speed/time graphs
- Graphs that enlighten and graphs that deceive
- End behavior of polynomials
- Ana paula winter
- Winter driving hazards
- Winter 1962 63
- Dr paul winter
- Porphyria's lover and farmers bride comparison
- Summer and winter uniform
- Walter winter desy
- Nouns for snow
- Punctate sialectasis meaning
- Winter summer autumn spring
- Tithonus themes
- Ninkly panadda
- Spring summer winter fall
- Summary of the poem dust of snow
- Welcome back from winter break
- Love’s austere and lonely offices
- Dr. hurm
- Winter law school elsa