950 likes | 1.13k Views
Graphs & Networks. Briana B. Morrison Adapted from Alan Eugenio, and William J. Collins. Topics. Definitions Storage Matrix Adjacency sets Algorithms Breadth first traversal Depth first traversal Spanning trees Shortest path Implementation. A graph is a pair ( V, E ) , where
E N D
Graphs & Networks Briana B. Morrison Adapted from Alan Eugenio, and William J. Collins
Topics • Definitions • Storage • Matrix • Adjacency sets • Algorithms • Breadth first traversal • Depth first traversal • Spanning trees • Shortest path • Implementation Graphs
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 positions and store elements 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 Graph 849 PVD 1843 ORD 142 SFO 802 LGA 1743 337 1387 HNL 2555 1099 1233 LAX 1120 DFW MIA Graphs
V = { S, T, X, Y, Z} E = { (X, Y), (X, Z), (Y, T), (T, Z), (T, S), (S, Z)} Graphs
EXAMPLE: X Y Z T 5 VERTICES 6 EDGES S Graphs
Edge Types • Directed edge • ordered pair of vertices (u,v) • first vertex u is the origin • second vertex v is the destination • e.g., a flight • Undirected edge • unordered pair of vertices (u,v) • e.g., a flight route • Directed graph • all the edges are directed • e.g., route network • Undirected graph • all the edges are undirected • e.g., flight network flight AA 1206 ORD PVD 849 miles ORD PVD Graphs
Applications • Electronic circuits • Printed circuit board • Integrated circuit • Transportation networks • Highway network • Flight network • Computer networks • Local area network • Internet • Web • Databases • Entity-relationship diagram Graphs
V a b h j U d X Z c e i W g f Y Terminology • End vertices (or endpoints) of an edge • U and V are the endpoints of a • Edges incident on a vertex • a, d, and b are incident on V • Adjacent vertices • U and V are adjacent • Degree of a vertex • X has degree 5 • Parallel edges • h and i are parallel edges • Self-loop • j is a self-loop Graphs
Terminology (cont.) • Path • sequence of alternating vertices and edges • begins with a vertex • ends with a vertex • each edge is preceded and followed by its endpoints • Simple path • path such that all its vertices and edges are distinct • Examples • P1=(V,b,X,h,Z) is a simple path • P2=(U,c,W,e,X,g,Y,f,W,d,V) is a path that is not simple V b a P1 d U X Z P2 h c e W g f Y Graphs
Terminology (cont.) • Cycle • circular sequence of alternating vertices and edges • each edge is preceded and followed by its endpoints • Simple cycle • cycle such that all its vertices and edges are distinct • Examples • C1=(V,b,X,g,Y,f,W,c,U,a,) is a simple cycle • C2=(U,c,W,e,X,g,Y,f,W,d,V,a,) is a cycle that is not simple V a b d U X Z C2 h e C1 c W g f Y Graphs
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet Graphs
X Y Z X, Y, T, Z, S HAS LENGTH 4. T SHORTEST PATH FROM X to S = ? S IN GENERAL, HOW CAN THE SMALLEST PATH BETWEEN 2 VERTICES BE DETERMINED? Graphs
X Y Z X, Y, T, Z, X IS A CYCLE. T Y, T, S, T, Y IS NOT A CYCLE. IS Y, Z, T, S, Z, X, Y A CYCLE? S Graphs
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet THIS UNDIRECTED GRAPH IS ACYCLIC. Graphs
Graph Categories • A graph is connected if each pair of vertices have a path between them • A complete graph is a connected graph in which each pair of vertices are linked by an edge Graphs
E D C B A Digraphs • A digraph is a graph whose edges are all directed • Short for “directed graph” • Applications • one-way streets • flights • task scheduling Graphs
Example of Digraph Graphs
Digraph Application • Scheduling: edge (a,b) means task a must be completed before b can be started ics21 ics22 ics23 ics51 ics53 ics52 ics161 ics131 ics141 ics121 ics171 The good life ics151 Graphs
Connectedness of Digraph • Strongly connected if there is a path from any • vertex to any other vertex. (no dead ends) • Weakly connected if, for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vi,vj). Graphs
Collection List Set ArrayList LinkedList SortedSet HashSet TreeSet Graphs
Graph Storage Two ways to store a graph: • Adjacency Matrix • Adjacency Set / List Graphs
Adjacency Matrix • An m by m matrix, called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (v,, vj). Its value is the weight of the edge, or -1 if the edge does not exist. Graphs
Adjacency Matrix (cont.) • Note that for an undirected graph, the matrix is symmetrical about the diagonal line. X Z Y T S Graphs
Adjacency Sets Graphs
Vertices and edges are positions store elements Accessor methods aVertex() incidentEdges(v) endVertices(e) isDirected(e) origin(e) destination(e) opposite(v, e) areAdjacent(v, w) Update methods insertVertex(o) insertEdge(v, w, o) insertDirectedEdge(v, w, o) removeVertex(v) removeEdge(e) Generic methods numVertices() numEdges() vertices() edges() Main Methods of the Graph ADT Graphs
Asymptotic Performance Graphs
Animation • Implementation Animation Graphs
Breadth-first search (BFS) is a general technique for traversing a graph A BFS traversal of a graph G Visits all the vertices and edges of G Determines whether G is connected Computes the connected components of G Computes a spanning forest of G BFS on a graph with n vertices and m edges takes O(n + m ) time BFS can be further extended to solve other graph problems Find and report a path with the minimum number of edges between two given vertices Find a simple cycle, if there is one Breadth-First Search Graphs