1 / 19

Graphs

Graphs. Topics to be discussed…. What is a graph? Directed vs. undirected graphs Trees vs graphs Terminology: Degree of a Vertex Graph terminology Graph Traversal Graph representation. What is a graph?.

adriel
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. Topics to be discussed… • What is a graph? • Directed vs. undirected graphs • Trees vs graphs • Terminology: Degree of a Vertex • Graph terminology • Graph Traversal • Graph representation

  3. What is a graph? • A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other • The set of edges describes relationships among the vertices

  4. Formal definition of graphs • A graph G is defined as follows: G=(V,E) V(G): a finite, nonempty set of vertices E(G): a set of edges (pairs of vertices) back

  5. Directed vs. undirected graphs • When the edges in a graph have no direction, the graph is called undirected

  6. Directed vs. undirected graphs (cont.) • When the edges in a graph have a direction, the graph is called directed (or digraph) Warning: if the graph is directed, the order of the vertices in each edge is important !! E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7) back

  7. Trees vs graphs • Trees are special cases of graphs!! back

  8. Terminology:Degree of a Vertex • The degree of a vertex is the number of edges incident to that vertex • For directed graph, • the in-degree of a vertex v is the number of edgesthat have v as the head • the out-degree of a vertex v is the number of edgesthat have v as the tail • if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is

  9. Examples 0 3 2 1 2 0 3 3 1 2 3 3 6 5 4 3 1 G1 1 1 3 G2 1 3 0 in:1, out: 1 directed graph in-degree out-degree in: 1, out: 2 1 in: 1, out: 0 2 G3 back

  10. Graph terminology • Adjacent nodes: two nodes are adjacent if they are connected by an edge • Path: a sequence of vertices that connect two nodes in a graph • Complete graph: a graph in which every vertex is directly connected to every other vertex

  11. Graph terminology (cont.) • What is the number of edges in a complete undirected graph with N vertices?  N * (N-1) / 2

  12. Graph terminology (cont.) • Weighted graph: a graph in which each edge carries a value back

  13. Graph Traversal • Problem: Search for a certain node or traverse all nodes in the graph • Depth First Search • Once a possible path is found, continue the search until the end of the path • Breadth First Search • Start several paths at a time, and advance in each one step at a time back

  14. Graph Representations • Adjacency Matrix • Adjacency Lists

  15. Adjacency Matrix • Let G=(V,E) be a graph with n vertices. • The adjacency matrix of G is a two-dimensional n by n array, say adj_mat • If the edge (vi, vj) is in E(G), adj_mat[i][j]=1 • If there is no such edge in E(G), adj_mat[i][j]=0 • The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric

  16. 0 4 1 2 5 6 3 7 0 0 1 2 3 1 2 G2 G1 symmetric undirected: n2/2 directed: n2 G4 back

  17. Adjacency Lists (data structures) Each row in adjacency matrix is represented as an adjacency list.

  18. 0 0 4 1 2 5 3 6 7 1 2 3 0 1 2 3 1 1 2 3 2 0 1 2 3 4 5 6 7 0 2 3 0 3 0 1 3 0 3 1 2 1 2 0 5 G1 0 6 4 0 1 2 1 5 7 1 0 2 6 G4 G3 2 back An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes

  19. Thank You

More Related