1 / 69

Graphs

Graphs. Graph Definition. A graph G=(V,E) consists a set of vertices , V, and edges , E. Each edge is a pair (v,w) where v,w are elements of V If the edge pair is ordered, then the graph is directed (digraph) and we use the notation <v,w>

keiki
Télécharger la présentation

Graphs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Graphs

  2. Graph Definition • A graph G=(V,E) consists a set of vertices, V, and edges, E. • Each edge is a pair (v,w) where v,w are elements of V • If the edge pair is ordered, then the graph is directed (digraph) and we use the notation <v,w> • Sometimes edges have a third component called cost

  3. Simple Graph • a pair (V,E) • V • finite set of vertices • points • E • irreflexive, symmetric relation on V • edges between vertices

  4. Multigraph • A graph than contains • more than one edge • from a vertex to another vertex

  5. More Definitions -Adjacency • If the edge (u,v) is in E then u and v are adjacent and the edge (u,v) is incident on u and v • If the edge <u,v> is in E then u is adjacent to v and v is adjacent from u • The number of edges incident on a vertex is the degree of the vertex • Digraph nodes had in-degree and out-degree

  6. Adjacent, Incident • Vertices u and v in undirected graph G are called adjacent if {u,v} is an edge of G • If edge e = {u,v}, e is called incident with vertices u and v • Edge econnectsu and v

  7. Degree of a vertex • the number of edges incident to (touching) the vertex deg(A) = 5

  8. Directed Edges • A directed edge (u,v) • shows a path from point u to point v • includes an arrow • u is called the initial vertex of (u,v) • v is called the terminal vertex of (u,v)

  9. In-Degree, Out-Degree • deg-(v) • the in-degree of vertex v • the number of edges with v as terminal vertex • deg+(v) • the out-degree of vertex v • the number of edges with v as initial vertex

  10. More Definitions -Paths • A path is a sequence of vertices, v1, v2,..., vn such that (vi, vi+1) is an element of E for 1<=i<=n • The length of the path is the number of edges in the path (n-1) • A simple path is a path with all distinct vertices (except perhaps the first and last)

  11. Number of Edges in a Graph • The maximum number of edges in a graph is n(n-1)/2 (where n is number of vertices) • The maximum number of edges in a digraph is n(n-1) • A graph with the maximum number of edges is a complete graph • A graph with few edges is sparse • A nearly complete graph is dense

  12. Complete Graph • A complete graph on n vertices: Kn • a simple graph • containing exactly one edge • between each pair of distinct vertices

  13. Directed graph • a pair (V,E) • V • finite set of vertices (points) • E is an relation on V • For any pair u,v in V, • E may contain one edge (u,v) from u to v, • and (v,u) another edge from v to u.

  14. Example Digraph A B D E C F G

  15. Example Digraph • V = {A, B, C, D, E, F, G} • E = {<A,F>,<A,B>,<C,A>,<F,C>,<B,F>, <G,B>,<B,D>,<D,E>,<E,G>} • (A,B,D,E,G) is a path of length 4 • (A,F,C,A) is a simple cycle • (A,B,D,E,G,B,F,C,A) is a cycle (not simple)

  16. More Definitions -Cycles • A cycle is a path of length at least one where the starting and ending vertices are the same • A cycle is a simple cycle if the path is a simple path • An acyclic graph is a graph with no cycles • A directed acyclic graph is sometimes called a DAG

  17. Cycles Cn • A cycle n vertices (n>=3) • {v1, v2, v3, … vn} • edges between {v1,v2}, {v2,v3}, …, {vn-1, vn}, {vn, v1}

  18. Bipartite Graph • A simple graph is bipartite • vertex set V can be partitioned into disjoint sets V1 and V2 • every edge in the graph connects a vertex in V1 and a vertex in V2

  19. Subgraph • A subgraph of graph G = (V,E) • is a graph H = (W,F) • where WÍV and FÍE

  20. Graph Union • The union of two simple graphs • G1 = (V1, E1) and G2 = (V2, E2) • G1È G2 • is a simple graph • with vertex set: V1È V2 • and edge set: E1È E2

  21. Graphs Representations

  22. Adjacency Matrix AG • A graph representation • A n´n zero-one matrix • A = [aij]

  23. Graph Representations - Adjacency Matrix • If G=(V,E) is a graph with n vertices, then G can be represented by a nxn, say A, where A[i,j]=1 iff (i,j) is in E (<i,j> for a digraph), and A[i,j]=0 if there is no such edge in E • Space requirements are n2 • O(1) to determine if (i,j) is in E • O(n2) to initialize, determine number of edges, etc.

  24. Adjacency Matrix Example A B C D E F G

  25. Adjacency List • A graph representation • which specifies all vertices that are adjacent • to each vertex of the graph

  26. Graph Representations -Adjacency Lists • If |E| << n2 then some operations can be more efficient if we use adjacency lists • For each vertex we keep a list of adjacent vertices • O(|E|) +O(|V|) space requirement • O(|E|) creation, scanning all edges, etc.

  27. B F D F A E G F C G B Adjacency List Example A B C D E

  28. Incidence Matrix AG • A graph representation • A n´m zero-one matrix • n vertices • m edges • M = [mij]

  29. Incidence Matrix AG

  30. Connectivity

  31. Path of length n from u to v(undirected graph) • a sequence of edges e1, e2, … en • such that • f (e1) = {x0, x1} • f (e2) = {x1, x2} • f (en) = {xn-1, xn} • where x0 = u & xn = v • The path is denoted by its vertex sequence • {x1, x2, …, xn}

  32. Path Examples • a,d,c,f,e • a simple path of length 4 • d,e,c,a • not a path • b,c,f,e,b • a circuit of length 4 • a,b,e,d,a,b • path, but not simple

  33. Circuit (cycle) • a path in a graph in which • first and last vertices • are the same vertex • Simple Circuit • no edges are repeated

  34. Connected graph • A graph in which there is • a path between every pair of distinct vertices

  35. Connected Components • A graph not connected • is the union of two or more connected subgraphs • called connected components

  36. Cut Vertices / Cut Edges • Cut Vertices • A vertex which when removed (with edges) • produces a subgraph with more connected components than in the original graph

  37. Strongly Connected DiGraph • a directed graph in which • there exists a path from a to b • and a path from b to a • whenever a and b are vertices in the graph

  38. Weakly Connected Digraph • a directed graph which contains a path • between any two vertices • in the underlying undirected graph

  39. Shortest Path Algorithms • Algorithms exist to find the shortest path between two vertices (Dijkstra)

  40. Planar graph • A graph drawn without any edges crossing • crossing of edges • lines intersect at some point • other than the common endpoint

  41. Breadth-First Graph Processing • Put the starting node on a queue of nodes to be processed • Remove the front node from the queues and process each of its adjacent nodes, putting them on the queue if they have not been visited before • Continue processing until no nodes remain on the queue • O(|E|)

  42. Depth-First Graph Processing • Start with a starting node • Visit each of the nodes adjacent to the current node, calling the depth-first search on each that has not been visited • O(|E|)

  43. Shortest Unweighted Path • Performs a breadth-first processing of the nodes, assigning each of them a cost which is the length of the path from the starting node to the node • Uses a Queue of vertices to be examined • Picking a vertex is O(|V|) for whole algorithm • Each edge is examined once during the algorithm so O(|E|) - dominate factor

  44. 0 A B D E C F G 0 1 2 A B D E C F G 1 Shortest Unweighted Path Example 0 1 A B D E C F G 1 0 1 2 A B D E C 2 F G 1

  45. Shortest Unweighted Path Example (cont) 0 1 0 1 2 2 A A B D B D E C E C 3 3 2 2 F G F G 1 1 0 1 2 0 1 A 2 B D A B D E C 3 E C 3 2 2 F G F G 1 4 1 4

  46. Weighted Shortest Path • In unweighted shortest path, each node has its distance set only once • If edges have positive weights, then may need to reset distances more than once

  47. Example of Cost Resetting 3 v 12 w 7 0 Start 8 w’s cost was set to 12 when node u visited. When node v is visited needs to be reset u 4

  48. Weighted Shortest Path Algorithm (Dijkstra’s) • Label the starting vertex with a cost of zero and pick as the current vertex • Label each of the vertex adjacent to current vertex with cost of current vertex plus cost of edge to the vertex • Pick the vertex with the smallest cost as the new current vertex • Continue 1 & 2 until no vertices remainhttp://carnap.ss.uci.edu/java/dijkstra/DijkstraApplet.html

  49. Dijkstra’s Algorithm Requirements • At each step we want vertex with smallest cost as next current vertex • If we scan table each time, this requires O(|V|2) operations • Use a priority queue which has nlogn complexity • Actual implementation permits duplicate vertices in priority queue

  50. 0 3 3 1 A B D 1 2 8 1 E 3 C 5 4 F G 3 8 0 3 4 0 3 4 5 6 6 Example of Dijkstra’s Algorithm 0 3 1 A B D 1 2 8 1 E 3 C 5 4 F G 3 3 1 A 3 1 B D A B D 1 1 2 8 2 8 1 E 3 1 C E 3 C 5 4 5 4 F G 3 F G 3 1

More Related