1 / 26

Graphs

Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite hierarchy; for example a family tree or a computer file system.

ackerman
Download Presentation

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 • All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite hierarchy; for example a family tree or a computer file system. • Some data need to have connections between items which do not fit into a hierarchy like this. Graph data structures can be useful in these situations. • A graph consists of a number of data items, each of which is called a vertex. Any vertex may be connected to any other, and these connections are called edges.

  2. Extremely useful tool in modelling problems • Consist of: • Vertices • Edges Vertices can beconsidered “sites”or locations. Edges represent connections. D E C A F Graphs B Vertex Edge

  3. Air flight system • Each vertex represents a city • Each edge represents a direct flight between two cities • A query on direct flights becomes a query on whether an edge exists • A query on how to get to a location is “does a path exist from A to B” • We can even associate costs to edges (weighted graphs), then ask “what is the cheapest path from A to B” Applications

  4. Wireless communication • Can be represented by a weighted complete graph (every two vertices are connected by an edge). • Each edge represents the Euclidean distancedij between two stations. • Each station uses a certain power i to transmit messages. Given this power i, only a few nodes can be reached (bold edges). A station reachable by i then use its own power to relay the message to other stations not reachable by i. • A typical wireless communication problem is: how to broadcast between allstations such that they are all connected and the power consumption is minimized. Another application

  5. Some other applications: • Electronic circuits: To find the least resistance between two nodes. • Computer networks: To find the smallest path between 2 computers. • Databases: To draw the entity relationship(ER) diagram.

  6. {a,b} {a,c} {b,d} {c,d} {b,e} {c,f} {e,f} • Undirected graph • An undirected graph is specified by an ordered pair (V,E), where V is the set of vertices and E is the set of edges Definition

  7. If v1 and v2 are connected, they are said to be adjacent vertices • v1 and v2 are endpoints of the edge {v1, v2} • If an edge e is connected to v, then v is said to be incident on e. Also, the edge e is said to be incident on v. • {v1, v2} = {v2, v1}* Terminology *Later, we will talk about “directed graphs”, where edges have direction. Thismeans that {v1,v2} ≠ {v2,v1} . Directed graphs are drawn with arrows (called arcs) between edges. This means {A,B} only, not {B,A} A B

  8. Two popular computer representations of a graph. Both represent the vertex set and the edge set, but in different ways. • Adjacency Matrix Use a 2D matrix to represent the graph • Adjacency List Use a 1D array of linked lists Graph Representation

  9. 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph • Each row and column is indexed by the vertex id. • - e,g a=0, b=1, c=2, d=3, e=4 • An array entry A [i] [j] is equal to 1 if there is an edge connecting • vertices i and j. Otherwise, A [i] [j] is 0. • The storage requirement is Θ(n2). Not efficient if the graph has few edges. • We can detect in O(1) time whether two vertices are connected. Adjacency Matrix

  10. The adjacency list is an array A[0..n-1] of lists, where n is the number of vertices in the graph. Each array entry is indexed by the vertex id (as with adjacency matrix)‏ • The list A[i] stores the ids of the vertices adjacent to i. Adjacency list

  11. 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 1 0 8 1 0 0 1 1 0 0 0 1 0 1 2 0 1 0 0 1 0 0 0 1 0 2 9 3 0 1 0 0 1 1 0 0 0 0 1 4 0 0 1 1 0 0 0 0 0 0 7 3 5 0 0 0 1 0 0 1 0 0 0 6 4 6 0 0 0 0 0 1 0 1 0 0 5 7 0 1 0 0 0 0 1 0 0 0 8 1 0 1 0 0 0 0 0 0 1 9 0 1 0 0 0 0 0 0 1 0 Examples

  12. 8 0 0 2 3 7 9 1 8 2 1 4 8 3 1 4 5 2 9 4 2 3 1 5 3 6 7 3 5 7 6 6 4 7 1 6 5 8 0 2 9 9 1 8 Examples

  13. Adjacency Lists • More compact than adjacency matrices if graph has few edges • Requires more time to find if an edge exists • Adjacency Matrix • Always require n2 space • This can waste a lot of space if the number of edges are sparse • Can quickly find if an edge exists Adjacency Lists vs. Matrix

  14. A path is a sequence of vertices (v0, v1, v2,… vk) such that: • For 0 ≤ i < k, {vi, vi+1} is an edge • For 0 ≤ i < k-1, vi ≠ vi+2 That is, the edge {vi, vi+1} ≠ {vi+1, vi+2} Note: a path is allowed to go through the same vertex or the same edge any number of times! • The length of a path is the number of edges on the path Path between vertices

  15. A path is simple if and only if it does not contain a vertex more than once. • A path is a cycle if and only if v0= vk • The beginning and end are the same vertex! • A path contains a cycle if some vertex appears twice or more Types of paths

  16. Are these paths? Any cycles? What is the path’s length? {a,c,f,e} {a,b,d,c,f,e} {a, c, d, b, d, c, f, e} {a,c,d,b,a} {a,c,f,e,b,d,c,a} Examples

  17. A graph is directed if direction is assigned to each edge. We call the directed edges arcs. • An edge is denoted as an ordered pair (u, v) • Recall: for an undirected graph • An edge is denoted {u,v}, which actually corresponds to two arcs (u,v) and (v,u) Directed Graph

  18. A path in a directed graph must follow the direction of the arrows. • A directed graph is connected if, for any pair of vertices, there is a path between them. Is the above graph connected? D C E A

  19. Representations

  20. A directed path is a sequence of vertices (v0, v1, . . . , vk) • Such that (vi, vi+1) is an arc • A directed cycle is a directed path such that the first and last vertices are the same. • A directed graph is acyclic if it does not contain any directed cycles Directed Acyclic Graph

  21. 3 6 8 0 7 2 9 1 5 4 Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges? Total OutDeg? Total Indeg? Example

  22. Directed graphs are often used to represent order-dependent tasks • That is we cannot start a task before another task finishes • We can model this task dependent constraint using arcs • An arc (i,j) means task j cannot start until task i is finished • Clearly, for the system not to hang, the graph must be acyclic. j i Directed Graphs Usage Task j cannot start until task i is finished

  23. Self loops: An edge from an vertex to itself is called self loop. • A complete graph is a graph which has maximum number of edges. • For undirected graph with n vertices it is n(n-1)/2. • For directed graph it is n(n-1). • Subgraph of G(V,E): Is a graph G’ =(v’,e’) where v’is subset of V and e’ is subset of E. Other Definitions

  24. Path: A path from vertex vi to vj is a sequence of vertices vi, vi1,vi2……vj such that <vi,vi1><vi1,vi2>…..<vin,vj> edges in the graph. • A simple path is a path in which all vertices except possibly the start and end are distinct. • A cycle is a simple path in which start and end vertex are same. • In an undirected graph two vertices vi and vj are said to be cnnected if there exists a path in G from vi to vj.

  25. Connected graph: An undirected graph is called connected if for every pair of vertices vi and vj there exists a path from vi to vj. • A connected component of an undirected graph is the maximal connected subgraph. • Tree is an acyclic graph • Strongly connected graph:a directed graph is said to be strongly connected if for every pair of vertices vi and vj in V(G) there is a directed path between vi and vj and also from vj and vi.

  26. Strongly connected component: • Is a maximal subgraph that is strongly connected. • Degree: degree of a vertex is number of edges incident on that vertex. • Indegree of v:Number of edges that has v as head in the directed graph. • Outdegree of v: Number of edges that has v as tail.

More Related