Sponsored Content
Full Discussion: [C] deleting node from graph
Top Forums Programming [C] deleting node from graph Post 302531930 by Loic Domaigne on Sunday 19th of June 2011 12:15:58 AM
Old 06-19-2011
Quote:
So I suppose doing it the opposite way is not correct, but why?
Because:
  • you're freeing G->adj[i], not G->adj[u].
  • If you would free G->adj[u], the pointer wouldn't be correct since you have already shifted.
You could perhaps get your code working by saving G->adj[u] before the shift; and then deleting it.

As your graph is undirected, there is many possible improvements. I'll try to post a few ideas tomorrow. But for now, it's bed time Smilie

Cheers, Loïc

---------- Post updated 06-19-11 at 06:15 AM ---------- Previous update was 06-18-11 at 10:31 PM ----------

Hi Luke,

here a few idea to improve the algorithm performance, given your data structure and the fact that the graph is undirected:
  • instead of visiting of node of the graph to check if it has an edge to the node to delete, it is more efficient to only visit the nodes given in the adjacency list of node u.
  • you don't need recursive function to remove the edge (u,v). Instead:
    Code:
    prev=NULL;
    for (p=G->adj[v]; p && p->key!=u; prev=p, p=p->next);
    if (prev==NULL) G->adj[v]=p->next; else prev->next = p->next;

  • you could perhaps try memmove() to shift the G->adj array. This should be equivalent to your shift operation, but eventually memmove() implementation is faster, because the libc designers usually take advantage of the underlying hardware (e.g. SSE instructions).
    Code:
    if (u!=G->nv-1) memmove(G->adj[u], G->adj[u+1], (G->nv-u-1)*sizeof(edge*));

I don't know however if your data structure is the most suited for what you're targeting, but perhaps can Mrs. Google help you further...

HTH, Loïc
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Graph generation

How can I generate graphs using perl in unix solaris environment? Please suggest. (2 Replies)
Discussion started by: wadhwa.pooja
2 Replies

2. AIX

Performance graph

Guru's I need to develop a graph which shows the CPU, memory and swap space utilization in a single graph against time I know of NMON but I am not able to make a single graph out of it. Does anyone know of any script or tool for data sampling and developing graph? Thanks in advance ... (1 Reply)
Discussion started by: balaji_prk
1 Replies

3. Shell Programming and Scripting

FS Growth Graph

It's been a while since i've been here.. and hopefully you can help me. I have created a script to get the filesystem utilization. Now i want to create a growth graph, which would show how much kb we increase per day. Here's the data 03-02-2010 00:00:00: /dev/md/dsk/d30 46473377 7355320... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

4. Solaris

SVM metaset on 2 node Solaris cluster storage replicated to non-clustered Solaris node

Hi, Is it possible to have a Solaris cluster of 2 nodes at SITE-A using SVM and creating metaset using say 2 LUNs (on SAN). Then replicating these 2 LUNs to remote site SITE-B via storage based replication and then using these LUNs by importing them as a metaset on a server at SITE-B which is... (0 Replies)
Discussion started by: dn2011
0 Replies

5. UNIX for Dummies Questions & Answers

Help with Plotting a graph using Perl

Dear all, I have a hash in my data file and I'd like to output the data contained within as a graph (can be lines or histogram form) but I dont know how to do this with Perl. Can somone suggest how I can have a graph with Keys of the hash being on the X-axis and the Values for the keys plotted on... (1 Reply)
Discussion started by: pawannoel
1 Replies

6. Programming

Graph in java

Hello, Please how can represent this graph in java ? i have to apply an algorithm on this graphe, the first instruction of the algorithm is to verify if the three last vertices are dependent or not ? Thank you. (1 Reply)
Discussion started by: chercheur857
1 Replies

7. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

8. Homework & Coursework Questions

Accessing one UNIX node from another node of the same server

Hi Experts, I am in need of running a script from one node say node 1 via node 2. My scheduling tool dont have access to node2 , so i need to invoke the list file from node1 but the script needs to run from node2. because the server to which i am hitting, is having access only for the node... (5 Replies)
Discussion started by: arun1377
5 Replies

9. HP-UX

Mount FIle systems from node-1 onto node-2

Hi, We have HP UX service guard cluster on OS 11.23. Recently 40+ LUNs presented to both nodes by SAN team but I was asked to mount them on only one node. I created required VGs/LVs, created VxFS and mounted all of them and they are working fine. Now client requested those FS on 2nd node as... (4 Replies)
Discussion started by: prvnrk
4 Replies
struct::graph1(3tcl)						Tcl Data Structures					      struct::graph1(3tcl)

__________________________________________________________________________________________________________________________________________________

NAME
struct::graph1 - Create and manipulate directed graph objects (version 1) SYNOPSIS
package require Tcl 8.2 package require struct::graph ?1.2.1? graphName option ?arg arg ...? graphName destroy graphName arc append arc ?-key key? value graphName arc delete arc ?arc ...? graphName arc exists arc graphName arc get arc ?-key key? graphName arc getall arc graphName arc keys arc graphName arc keyexists arc ?-key key? graphName arc insert start end ?child? graphName arc lappend arc ?-key key? value graphName arc set arc ?-key key? ?value? graphName arc source arc graphName arc target arc graphName arc unset arc ?-key key? graphName arcs ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist? graphName node append node ?-key key? value graphName node degree ?-in|-out? node graphName node delete node ?node ...? graphName node exists node graphName node get node ?-key key? graphName node getall node graphName node keys node graphName node keyexists node ?-key key? graphName node insert ?child? graphName node lappend node ?-key key? value graphName node opposite node arc graphName node set node ?-key key? ?value? graphName node unset node ?-key key? graphName nodes ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist? graphName get ?-key key? graphName getall graphName keys graphName keyexists ?-key key? graphName set ?-key key? ?value? graphName swap node1 node2 graphName unset ?-key key? graphName walk node ?-order order? ?-type type? ?-dir direction? -command cmd _________________________________________________________________ DESCRIPTION
The ::struct::graph command creates a new graph object with an associated global Tcl command whose name is graphName. This command may be used to invoke various operations on the graph. It has the following general form: graphName option ?arg arg ...? Option and the args determine the exact behavior of the command. A directed graph is a structure containing two collections of elements, called nodes and arcs respectively, together with a relation ("con- nectivity") that places a general structure upon the nodes and arcs. Each arc is connected to two nodes, one of which is called the source and the other the target. This imposes a direction upon the arc, which is said to go from the source to the target. It is allowed that source and target of an arc are the same node. Such an arc is called a loop. Whenever a node is source or target of an arc both are said to be adjacent. This extends into a relation between nodes, i.e. if two nodes are connected through at least one arc they are said to be adjacent too. Each node can be the source and target for any number of arcs. The former are called the outgoing arcs of the node, the latter the incoming arcs of the node. The number of edges in either set is called the in- resp. the out-degree of the node. In addition to maintaining the node and arc relationships, this graph implementation allows any number of keyed values to be associated with each node and arc. The following commands are possible for graph objects: graphName destroy Destroy the graph, including its storage space and associated command. graphName arc append arc ?-key key? value Appends a value to one of the keyed values associated with an arc. If no key is specified, the key data is assumed. graphName arc delete arc ?arc ...? Remove the specified arcs from the graph. graphName arc exists arc Return true if the specified arc exists in the graph. graphName arc get arc ?-key key? Return the value associated with the key key for the arc. If no key is specified, the key data is assumed. graphName arc getall arc Returns a serialized list of key/value pairs (suitable for use with [array set]) for the arc. graphName arc keys arc Returns a list of keys for the arc. graphName arc keyexists arc ?-key key? Return true if the specified key exists for the arc. If no key is specified, the key data is assumed. graphName arc insert start end ?child? Insert an arc named child into the graph beginning at the node start and ending at the node end. If the name of the new arc is not specified the system will generate a unique name of the form arcx. graphName arc lappend arc ?-key key? value Appends a value (as a list) to one of the keyed values associated with an arc. If no key is specified, the key data is assumed. graphName arc set arc ?-key key? ?value? Set or get one of the keyed values associated with an arc. If no key is specified, the key data is assumed. Each arc that is added to a graph has the empty string assigned to the key data automatically. An arc may have any number of keyed values associated with it. If value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key. graphName arc source arc Return the node the given arc begins at. graphName arc target arc Return the node the given arc ends at. graphName arc unset arc ?-key key? Remove a keyed value from the arc arc. If no key is specified, the key data is assumed. graphName arcs ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist? Return a list of arcs in the graph. If no restriction is specified a list containing all arcs is returned. Restrictions can limit the list of returned arcs based on the nodes that are connected by the arc, on the keyed values associated with the arc, or both. The restrictions that involve connected nodes have a list of nodes as argument, specified after the name of the restriction itself. -in Return a list of all arcs whose target is one of the nodes in the nodelist. -out Return a list of all arcs whose source is one of the nodes in the nodelist. -adj Return a list of all arcs adjacent to at least one of the nodes in the nodelist. This is the union of the nodes returned by -in and -out. -inner Return a list of all arcs adjacent to two of the nodes in the nodelist. This is the set of arcs in the subgraph spawned by the specified nodes. -embedding Return a list of all arcs adjacent to exactly one of the nodes in the nodelist. This is the set of arcs connecting the sub- graph spawned by the specified nodes to the rest of the graph. -key key Limit the list of arcs that are returned to those arcs that have an associated key key. -value value This restriction can only be used in combination with -key. It limits the list of arcs that are returned to those arcs whose associated key key has the value value. The restrictions imposed by either -in, -out, -adj, -inner, or -embedded are applied first. Specifying more than one of them is illegal. At last the restrictions set via -key (and -value) are applied. Specifying more than one -key (and -value) is illegal. graphName node append node ?-key key? value Appends a value to one of the keyed values associated with an node. If no key is specified, the key data is assumed. graphName node degree ?-in|-out? node Return the number of arcs adjacent to the specified node. If one of the restrictions -in or -out is given only the incoming resp. outgoing arcs are counted. graphName node delete node ?node ...? Remove the specified nodes from the graph. All of the nodes' arcs will be removed as well to prevent unconnected arcs. graphName node exists node Return true if the specified node exists in the graph. graphName node get node ?-key key? Return the value associated with the key key for the node. If no key is specified, the key data is assumed. graphName node getall node Returns a serialized list of key/value pairs (suitable for use with [array set]) for the node. graphName node keys node Returns a list of keys for the node. graphName node keyexists node ?-key key? Return true if the specified key exists for the node. If no key is specified, the key data is assumed. graphName node insert ?child? Insert a node named child into the graph. The nodes has no arcs connected to it. If the name of the new child is not specified the system will generate a unique name of the form nodex. graphName node lappend node ?-key key? value Appends a value (as a list) to one of the keyed values associated with an node. If no key is specified, the key data is assumed. graphName node opposite node arc Return the node at the other end of the specified arc, which has to be adjacent to the given node. graphName node set node ?-key key? ?value? Set or get one of the keyed values associated with a node. If no key is specified, the key data is assumed. Each node that is added to a graph has the empty string assigned to the key data automatically. A node may have any number of keyed values associated with it. If value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key. graphName node unset node ?-key key? Remove a keyed value from the node node. If no key is specified, the key data is assumed. graphName nodes ?-key key? ?-value value? ?-in|-out|-adj|-inner|-embedding nodelist? Return a list of nodes in the graph. Restrictions can limit the list of returned nodes based on neighboring nodes, or based on the keyed values associated with the node. The restrictions that involve neighboring nodes have a list of nodes as argument, specified after the name of the restriction itself. The possible restrictions are the same as for method arcs. The set of nodes to return is computed as the union of all source and target nodes for all the arcs satisfying the restriction as defined for arcs. graphName get ?-key key? Return the value associated with the key key for the graph. If no key is specified, the key data is assumed. graphName getall Returns a serialized list of key/value pairs (suitable for use with [array set]) for the whole graph. graphName keys Returns a list of keys for the whole graph. graphName keyexists ?-key key? Return true if the specified key exists for the whole graph. If no key is specified, the key data is assumed. graphName set ?-key key? ?value? Set or get one of the keyed values associated with a graph. If no key is specified, the key data is assumed. Each graph has the empty string assigned to the key data automatically. A graph may have any number of keyed values associated with it. If value is not specified, this command returns the current value assigned to the key; if value is specified, this command assigns that value to the key. graphName swap node1 node2 Swap the position of node1 and node2 in the graph. graphName unset ?-key key? Remove a keyed value from the graph. If no key is specified, the key data is assumed. graphName walk node ?-order order? ?-type type? ?-dir direction? -command cmd Perform a breadth-first or depth-first walk of the graph starting at the node node going in either the direction of outgoing or opposite to the incoming arcs. The type of walk, breadth-first or depth-first, is determined by the value of type; bfs indicates breadth-first, dfs indicates depth-first. Depth-first is the default. The order of the walk, pre-order, post-order or both-order is determined by the value of order; pre indicates pre-order, post indi- cates post-order, both indicates both-order. Pre-order is the default. Pre-order walking means that a node is visited before any of its neighbors (as defined by the direction, see below). Post-order walking means that a parent is visited after any of its neigh- bors. Both-order walking means that a node is visited before and after any of its neighbors. The combination of a bread-first walk with post- or both-order is illegal. The direction of the walk is determined by the value of dir; backward indicates the direction opposite to the incoming arcs, forward indicates the direction of the outgoing arcs. As the walk progresses, the command cmd will be evaluated at each node, with the mode of the call (enter or leave) and values graph- Name and the name of the current node appended. For a pre-order walk, all nodes are entered, for a post-order all nodes are left. In a both-order walk the first visit of a node enters it, the second visit leaves it. BUGS, IDEAS, FEEDBACK This document, and the package it describes, will undoubtedly contain bugs and other problems. Please report such in the category struct :: graph of the Tcllib SF Trackers [http://sourceforge.net/tracker/?group_id=12883]. Please also report any ideas for enhancements you may have for either package and/or documentation. KEYWORDS
cgraph, graph CATEGORY
Data structures COPYRIGHT
Copyright (c) 2002 Andreas Kupries <andreas_kupries@users.sourceforge.net> struct 1.2.1 struct::graph1(3tcl)
All times are GMT -4. The time now is 09:58 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy