Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 Graphs Extremely useful tool in modelling problems Consist of:
Vertices Edges Vertices can be considered “sites” or locations. Edges represent connections. D E C A F B Vertex Edge

3 Applications Each vertex represents a city
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”

4 Another application Wireless communication
Can be represented by a weighted complete graph (every two vertices are connected by an edge). Each edge represents the Euclidean distance dij 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 all stations such that they are all connected and the power consumption is minimized.

5 To find the least resistance between two nodes. Computer networks:
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 Definition 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 {c,f} {a,c} {a,b} {b,d} {c,d} {e,f} {b,e}

7 Terminology 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}* *Later, we will talk about “directed graphs”, where edges have direction. This means 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 Graph Representation 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

9 Adjacency Matrix Each row and column is indexed by the vertex id.
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.

10 Adjacency list 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.

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

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

13 Adjacency Lists vs. Matrix
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

14 Path between vertices 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

15 Types of paths 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

16 Examples 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}

17 A graph is directed if direction is assigned to each edge
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 Directed Acyclic Graph
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 Example 1 2 3 4 5 6 7 8 9 Indeg(2)? Indeg(8)? Outdeg(0)? Num of Edges?
1 2 3 4 5 6 7 8 9 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.


Download ppt "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."

Similar presentations


Ads by Google